Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to iterate a hashmap with key hashmap

 
Evelina Dimova
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I need help. I have the following hashmap:
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:

HashMap<Dimension, String> singleValues = new HashMap<Dimension, String>();
for (Map.Entry<HashMap<Dimension, Integer>, String> entry : mapList.entrySet()) {
Iterator iter = entry.getKey().entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<Dimension, Integer> innerEntry = (Map.Entry<Dimension, Integer>) iter.next();
if (innerEntry.getValue().equals(1)) {
singleValues.put(innerEntry.getKey(), entry.getValue());
System.out.println(innerEntry.getKey() + " : " +
entry.getValue());

}
}
}

Thanks in advance
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this . i didnt check this . if any problem try to solve it

 
Evelina Dimova
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, it works successfully. With a little change. But I still have a problem with the hashmap. The result I saw at the console is the same and it is repeating many manyyy times. I tought that the problem is with printing because I checked the information that I have already set at tha big hash map:

HashMap<HashMap<Dimension, Integer>, String> mapList = new HashMap<HashMap<Dimension, Integer>, String>();

Thanks for the quick answer!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Java; this isn't Struts-related.
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My first reaction to you key type "HashMap<Dimension, Integer>" was "What the heck..". Have you considered the point that key should be immutable? Is your key contents are final and unmodifiable ? I think, if you will rearrange Dimension, Integer and String a little bit from datastructure point of view, you can have simple and robust design.
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul is right. If you put only one more key-value pair into an inner HashMap it's hashCode will change, and it the outer HashMap will look into the wrong bucket.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic