Reputation: 143
Data Class contains two fields price (double), operator (string), I mapped the Data class as multiple values to each key of the map shown below as 1).
I am unable to show in the the code 2). all the values that are mapped under the same key,
Example, for Key=1, map has Values as Price {0.2,0.3,0.4} , Operator: {A,B,C}
the output of this code, can only show me price= 0.2, Operator: A for Key 1. all other values are not shown. how to solve it?
1) Map<Integer, ArrayList<Data>> mp = new HashMap<Integer, ArrayList <SortData>>();
2)
ArrayList<Data> ls = mp.get (keys.get(k));
int i=0;
for ( Data e: ls)
{
System.out.println(e.getOperator() + e.getPrice());
i++;
}
Code of Adding Data in the Map:
enter code here
ArrayList<Test> list = new ArrayList<Test>();
Map<Integer,ArrayList<Test>> mp = new HashMap<Integer,ArrayList<Test>>();
list.add(new Data(0,1,"A"));
list.add(new Data(0,2,"B"));
mp.put(1,list);
List<Test> value = mp.get(1);
value.add(0.3,"c");
value.add(0,5,"E");
Upvotes: 0
Views: 145
Reputation: 5344
In your code for adding values to the map - Compilation error. You cannot add Data objects to ArrayList.
Your iteration code is correct. So I believe the values simply doesn't exists in the Map. So review all code remove values from the list. And be sure in your add values code you add values to the same instance stored in the Map.
Nevertheless to create less errorphone code consider using Guava MultiMap
Upvotes: 1