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());
try this . i didnt check this . if any problem try to solve it
Evelina Dimova
Greenhorn
Joined: Oct 07, 2009
Posts: 2
posted
0
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:
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.
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.