Connecting Tech Pros Worldwide Forums | Help | Site Map

how to iterate a hashmap with key hashmap

Newbie
 
Join Date: Oct 2009
Posts: 1
#1: Oct 7 '09
Hello, I need help. I have the following hashmap:

Expand|Select|Wrap|Line Numbers
  1. HashMap<HashMap<Dimension, Integer>, String> mapList = new HashMap<HashMap<Dimension, Integer>, String>();
I want to extract Dimesion from the key, where the Integer is "1", and String from the value.

How could I iterate it?
I wrote that, but it doesn't work at the way I expected:

Expand|Select|Wrap|Line Numbers
  1. HashMap<Dimension, String> singleValues = new HashMap<Dimension, String>();
  2. for (Map.Entry<HashMap<Dimension, Integer>, String> entry : mapList.entrySet()) {
  3.   Iterator iter = entry.getKey().entrySet().iterator();
  4.   while (iter.hasNext()) {
  5.     Map.Entry<Dimension, Integer> innerEntry = (Map.Entry<Dimension, Integer>) iter.next();
  6.     if (innerEntry.getValue().equals(1)) {
  7.       singleValues.put(innerEntry.getKey(), entry.getValue());
  8.       System.out.println(innerEntry.getKey() + " : " +
  9.       entry.getValue());
  10.  
  11.     }
  12.   }
  13. }
Thanks in advance

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Oct 7 '09

re: how to iterate a hashmap with key hashmap


Your code smells a bit. Why do you have a Map<Map>? Also trying to search the map by value rather than the key adds a bit of bad odour.
What problem are these maps trying to solve?
Reply