I am new to collections. I need to know how I can use an ArrayList with two entries.
I am writing a program that reads a file and stores a string into a treemap and associating the string to an ArrayList with two columns(document id, count). I am wondering how I can do this. Can someone help me with some guidance on how I can do this? Here is how the system should be working:
For Eg:
If the program reads the word "Hello", the words need to be added to the treemap as Treemap("Hello", Arraylist).
The word Hello is associated with (DocumentID, Count) in an ArrayList.
In arraylist , we can't store in key value pair so instead of arrayList you can use pair.
Pair <String, Integer> pair = new Pair <String, Integer> ("DocumentId", count);
TreeMap("Hello", pair);
You can't put two values into an ArrayList. You could create a new class to store documentId and count. And then store this new Class in the List in the TreeMap:
public class CountPerDocument {
private final String documentId;
private final int count;
CountPerDocument(String documentId, int count){
this.documentId = documentId;
this.count = count;
}
public String getDocumentId() {
return documentId;
}
public int getCount() {
return count;
}
}
And fill your Map like this
Map<String, List<CountPerDocument>> map = new TreeMap<>();
List<CountPerDocument> list = new ArrayList<>();
//add code to fill your list here
list.add(new CountPerDocument("yourId", 23));
map.put("term", list);
Related
I'm making a network call that gives me a list of CoinPriceQueryResult objects:
public class CoinPriceQueryResult {
private String coinQueryId;
private double price;
}
How can I sort this list into the order of my second list of objects:
public class CoinStatus {
private String coinQueryId;
private int position;
}
based on the position.
List<CoinQueryResult> listQueryResult = new ArrayList();
listQueryResult.add("penny", 1.15);
listQueryResult.add("nickel", 5.05);
listQueryResult.add("dime", 10.10);
List<CoinStatus> listCoinStatus = new ArrayList();
listCoinStatus.add("nickel", 0);
listCoinStatus.add("penny", 1);
listCoinStatus.add("dime", 2);
I want listQueryResult to be sorted in the order of
nickel, penny, dime
How do I get the sorted list using the int value positionin the CoinStatus object?
First, create a Map that maps each coin name to its position/priority. Then, use Comparator.comparing to sort based on that Map.
Map<String, Integer> position = listCoinStatus.stream().collect(
Collectors.toMap(CoinStatus::getCoinQueryId, CoinStatus::getPosition));
listQueryResult.sort(Comparator.comparing(r -> position.get(r.getCoinQueryId())));
I have a map with titles. I want to print 10 random keys from my hashmap.
For example my map (String, Object) contains 100 pairs: "A, new Object(...)", "B, ...", "C, ..." etc.
I want to get 10 random keys from this map and append it to one string.
So my string should looks like: "A\nD\nB".
A quick way to get random 10 keys without repetition is putting the keys in a list and using Collections.shuffle to shuffle the list.
Map<String, Object> map = ...yourmap
ArrayList<String> keys = new ArrayList<>(map.keySet());
Collections.shuffle(keys);
List<String> randomTenKeys = keys.subList(0, 10);
Creating a list of all keys and shuffling it is not the most efficient thing you can do. You can do it in a single pass with a reservoir sampling algorithm. I haven't looked into it but you can probably find an implementation in some Apache or Guava library.
Joni's answer is quite good and short. But, here is a fully working example if you'd like. I split your problem into two methods - one to return a list of randomly selected keys and another to print keys in whichever way you like. You could combine the two methods into one. But, its better to keep them separate.
import java.util.*;
import java.util.stream.IntStream;
public class Test {
public static void main(String [] args){
Map<String, Object> map = new HashMap<>();
//You can use for loop instead to make a map of String, Integer.
IntStream.rangeClosed(0, 9).forEach(i -> map.put(i +"", i));//Map of 10 numbers.
List<String> keys = getRandomKeys(map, 3);
String allKeys = combineKeys(keys, "\n");
System.out.println(allKeys);
}
public static List<String> getRandomKeys(Map<String, Object> map, int keyCount) {
List<String> keys = new ArrayList<>(map.keySet());
for(int i = 0; i < map.size()-keyCount; i++){
int idx = (int) ( Math.random() * keys.size() );
keys.remove(idx);
}
return keys;
}
public static String combineKeys(List<String> keys, String separator){
String all = "";
for(int i = 0; i < keys.size() - 1; i++){
all = all + keys.get(i) + separator;
}
all += keys.get(keys.size()-1);//last element does not need separator.
return all;
}
}
HashMap Stores the values already in unsorted order it is random.
you can directly use
for(Map.Entry entry : map.entrySet())
str.append(entry.getKey()+" "+entry.getValue());
however if you want new order every time you can shuffle your data.
For Shuffle you need to get all keys in a array or list
Then you can shuffle that list and iterate over that list to get values from hashmap
This is a complementary answer to Joni's answer. Use String:join to join the randomTenKeys.
Given below is Joni's answer:
Map<String, Object> map = ...yourmap
ArrayList<String> keys = new ArrayList<>(map.keySet());
Collections.shuffle(keys);
List<String> randomTenKeys = keys.subList(0, 10);
and the complementary answer is:
String joinedKeys = String.join("\n", randomTenKeys);
Set<String> keys = myMap.keySet();
String combined = "";
for (int i=0; i<10; i++)
{
int random = (int)(Math.random() * keys.size());
String key = keys.get(random);
combined += key + "\n";
keys.remove(random);
}
I have a question regarding csv and java.
I have a csv file with a lot of data. There are for example three different ids, and per id around 5k data points.
The csv is like following:
ID, Data1(String), Data2(Long), Data3(Float), Data4(Float), Data5(Float)
I read the csv an store every column to another linked list, so that for ever linked list the index i is one row.
What I want to do is to have a per ID a list of the data.
I have done it so far like this:
LinkedList<String> new_id = new LinkedList();
LinkedList<String> new_data1 = new LinkedList();
LinkedList<String> new_data2 = new LinkedList();
....
....
for(int i = 0; i < id.size(); i++){
if(id.get(i).equals(String theidiwant){
new_id.add(id.get(i));
new_data1.add(data1.get(i));
....
....
}
So my question would be whether there is a simple solution to create for each id a list of those data.
I hope you understand what I am trying to ask.
Sorry for that complicated description.
Cheers!
You should start of by creating a class for the rows and have an object representing one row e.g.
public class Row{
public String id;
public String data1;
public long data2;
public float data3;
public float data4;
public float data5;
public Row(String id, String data1, long data2, float data3, float data4, float data5) {
//Enter constructor here e.g. this.id = id...
}
}
You can then filter the objects into a list with
... after parsing the data into List<Row> data;
List<Row> filteredList = data.stream().filter(
row -> row.id.equals(String theidiwant))
.collect(Collectors.toCollection(LinkedList::new));
If you want one list per id you can as SpaceCowboy said and create a map with lists
Map<String, List<Row>> mappedValues = new HashMap<>();
for(Row row : data) {
if(mappedValues.containsKey(row.id) {
List<Row> values = mappedValues.get(row.id)
values.add(row)
}
else {
List<Row> newValues = new ArrayList<>();
newValues.add(row)
mappedValues.put(row.id, newValues);
}
}
I am not sure why you want to create list of columns, list of rows make much more sense. Although, using java class to map the columns and creating a list of rows will be most appropriate.
You might want to try out using a Map.
Map<Integer, List<String>> myMap = new HashMap<Integer, List<String>>();
LinkedList<String> data = new LinkedList<>();
myMap.put([your_key],data);
You can then access the list with the id's like this:
if (myMap.containsKey([your_key])) {
List<String> list = myMap.get([your_key]);
}
I need to save pairs (string,object) into a hashmap. Basically I managed to populate the hashmap but I don't know how to access the values stored into memory.
This is my code:
HashMap<String, speedDial> SDs = new HashMap<String, speedDial>();
speedDial sd = new speedDial();
SDs.put(String.valueOf(temp),sd); whre temp is my index and sd my object
Then I fill in data into the sd reading them from an xml file.
When I debug the project with eclypse I can see the values are stored correctly into memory, but I've no idea how to retrive the string values associated to the object, see below the SD object format
class speedDial{
String label, dirN;
speedDial (String l, String dN) {
this.label = l;
this.dirN = dN;
}
}
See the picture below: it highlights the data I'm trying to access!
enter image description here
When I try to access the hashmap and print it's values I only got the last one, I use the following:
for ( int k = 0; k <50; k++) {
speedDial.printSD(SDs.get(String.valueOf(k)));
}
This is my printSD method taken from the speedDial class:
public static void printSD (speedDial SD) {
System.out.println("Dir.N: " + SD.dirN + " Label: " + SD.label);
}
And this is the output for all the 50 iterations, that is the last element I added to the hashmap in another for cycle that reads from a xml file.
Dir.N: 123450 Label: label5
Given a HashMap such as:
SpeedDial speedDial1 = new SpeedDial("test1", "test2");
SpeedDial speedDial2 = new SpeedDial("test3", "test4");
SpeedDial speedDial3 = new SpeedDial("test5", "test6");
HashMap<String, SpeedDial> exampleHashMap = new HashMap<>(3);
exampleHashMap.put("key1", speedDial1);
exampleHashMap.put("key2", speedDial2);
exampleHashMap.put("key3", speedDial3);
You can retrieve the value for a given key like so:
SpeedDial exampleOfGetValue = exampleHashMap.get("key1");
System.out.println(exampleOfGetValue.label);
System.out.println(exampleOfGetValue.dirN);
This outputs:
test1
test2
If you want to retrieve the keys for a given value then you could use something like:
public final <S, T> List<S> getKeysForValue(final HashMap<S, T> hashMap, final T value) {
return hashMap.entrySet()
.stream()
.filter(entry -> entry.getValue().equals(value))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}
If you call this function like so:
List<String> exampleOfGetKeys = getKeysForValue(exampleHashMap, speedDial1);
System.out.println(exampleOfGetKeys);
It would output a list of all keys that have this value:
[key1]
The following code will iterate through the map and will store the key and values in two lists.
List<String> keys = new ArrayList();
List<Object> values = new ArrayList();
for (Map.Entry<String, Object> speedDial: SDs.entrySet()) {
Object speedDialValue = speedDial.getValue();
String key= speedDial.getKey();
keys.add(key);
values.add(speedDialValue);
}
To retrieve the String value, typically getters are used as it is recommended to use the private modifier for your class attributes.
public class speedDial{
private String label, dirN;
public speedDial (String l, String dN) {
this.label = l;
this.dirN = dN;
}
public String getLabel(){
return this.label;
}
public String getDirN(){
return this.dirN;
}
}
The you can simply use yourObject.getLabel(); or yourObject.getDirN();
Hope that helps!
SDs.keySet() Gives you the Set of the keys of your HashMap
You can have the list of values using
for (String mapKey : SDs.keySet()) {
System.out.println("key: "+mapKey+" value: "+ SDs.get(mapKey).toString());
}
Yous have to write a toString() fonction for your speedDial
I have the following file named ght.txt in my c: and it contains the following data
Id|ytr|yts
1|W|T
2|W|T
3|W|T
Now the thing is that positions of this columns (Id|ytr|yts) is also not in order means they can be reshuffled also..for ex
Id|ytr|dgfj|fhfjk|fgrt|yts
or they can be as ..
Id|wer|ytr|weg|yts
so I have done the following way and read them in java as shown below
String[] headers = firstLine.split("|");
int id, Ix, Ixt, count = 0;
for(String header : headers) {
if(header.equals("Id")) {
idIx = count;
}elseif (header.equals("Ix")) {
Ixt = count;
} elseif (header.equals("Ixt")) {
Ixt = count;
}
count++;
}
Now I need to store them in a map in such a way that against id I will get the value of column ytr and yts so in map there should be single key but against that key value could be multiple please advise how to store in map in such a way
Using a Map<Integer,List<String>> sounds like a viable first approach.
As it sound like your value is structured, it might be even better to create a value class to hold this, eg. Map<Integer, YourValueClass> where
class YourValueClass
{
String ix;
String ixt;
// constructor, getters and setters
}
Basically, you should think in terms of classes/objects - don't be in object denial :-)
Cheers,
I'm not quite sure what you mean, but if I get it right, you are looking for a multimap.
You can roll one yourself, as #Anders R. Bystrup suggests.
Or you can use an existing implementation like the Google Collections Multimap.
Don't store one key and multiple values. Instead, you can store a Key and Values as a List.
You can use MultiMap from Guava Library:
MultiMap<String,String> map = ArrayListMultimap.create();
map.put("key","value1");
map.put("key","value2");
By using:
System.out.println(map.get("key");
Prints:
["value1","value2"]
Value Class
class TextValues {
final int id;
final String ix;
final String ixt;
private TextValues(final int id, final String ix, final String ixt){
this.id = id;
this.ix = ix;
this.ixt = ixt;
}
public static TextValues createTextValues(int id, String ix, String ixt) {
return new TextValues(id, ix, ixt);
}
}
Usage:
Map<Integer, TextValues> map = new HashMap<Integer, TextValues>();
map.put(1, TextValues.createTextValues(1, "ix value ", "ixt value"));
public static void main(String[] args) {
Map<String, List<String>> map = new HashMap<String, List<String>>();
List<String> valSetOne = new ArrayList<String>();
valSetOne.add("ABC");
valSetOne.add("BCD");
valSetOne.add("DEF");
List<String> valSetTwo = new ArrayList<String>();
valSetTwo.add("CBA");
valSetTwo.add("DCB");
map.put("FirstKey", valSetOne);
map.put("SecondKey", valSetTwo);
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
String key = entry.getKey();
List<String> values = entry.getValue();
System.out.println("Value of " + key + " is " + values);
}
}
You can use Set or List based on your requirement i.e you need elements in ordered or unordered collection.This is a simple method of having single key with multiple values.