aspose file tools*
The moose likes Java in General and the fly likes how to iterate a hashmap with key hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to iterate a hashmap with key hashmap" Watch "how to iterate a hashmap with key hashmap" New topic
Author

how to iterate a hashmap with key hashmap

Evelina Dimova
Greenhorn

Joined: Oct 07, 2009
Posts: 2
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

Joined: Jan 28, 2008
Posts: 5575

try this . i didnt check this . if any problem try to solve it

Evelina Dimova
Greenhorn

Joined: Oct 07, 2009
Posts: 2
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

Joined: Sep 29, 2008
Posts: 12617

Moving to Java; this isn't Struts-related.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
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

Joined: Oct 27, 2005
Posts: 19759
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
 
subject: how to iterate a hashmap with key hashmap