Forums Register Login

how to iterate a hashmap with key hashmap

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
try this . i didnt check this . if any problem try to solve it

+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
Moving to Java; this isn't Struts-related.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
We're being followed by intergalactic spies! Quick! Take this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2226 times.
Similar Threads
sysntex error in 2 dim array value assignment...
ClassCastException
A code to print the keys from two different hashMap having same values
runtime exception during accessing elements in hashmap
re: Iteration in a Map
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:36:59.