• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashMap<Integer, ArrayList>  RSS feed

 
Ananth Ram
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I need to find the size of the arraylist within a hashmap where the key of the hashmap is value of another hashmap.

// Declare the first one
Map<Integer, List<Integer>> m_Map1 = new HashMap<Integer, List<Integer>>();
// Add values
m_Map1.put(1, new ArrayList<Integer>(list1));
m_Map1.put(2, new ArrayList<Integer>(list2));

// Declare another one
Map<Integer, List<String>> m_Map2 = new HashMap<Integer, List<String>>();
// Adding values
m_Map2.put(0, new ArrayList<String>(list1));
m_Map2.put(1, new ArrayList<String>(list2));

For every map1 key, the values in the list correspond to map2 key.

I need to get the list size of map2 based on the values of map1.

Let me know if need you more info.

I started doing this and throwing some exception.


Set set = m_Map2.entrySet();
Iterator iterator1 = set.iterator();
for(Entry<Integer, List<Integer>> entry : m_Map1.entrySet()) {
System.out.println("Key = " + entry.getKey());
for(Iterator<Integer> iter = entry.getValue().iterator(); iter.hasNext();) {
System.out.println("values = " + iter.next());
// compare the key and get the list size then add to the topic count
while(iterator1.hasNext()) {
Map.Entry me = (Map.Entry)iterator1.next();
// what to do ??
}
}
}



Any help.

Thanks,
Ananth Ram



 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags.

Also, TellTheDetails -- what exception did you get?
 
tushar bhasme
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let me understand the question correctly...

there are 2 maps- m1 and m2...
by saying "For every map1 key, the values in the list correspond to map2 key. ", do you mean each key in map m1 points to a list that stores the keys of Map m2?
If this is correct, ,are you taking each value from the Key Set of Map m2 and adding them to a list? If so, instead you could directly store the set in the Map m1 where each key will point to a Key Set of a map m2...

if i did understand correct, here is what you could try:





 
Ananth Ram
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tushar,

Yes, it works.

Thanks,

Ananth Ram
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!