Tuesday, 10 September 2013

Looping Hash Map does not return correct Key

Looping Hash Map does not return correct Key

I have a hash map type array list:
public static ArrayList<HashMap<String, String>> mylist = new
ArrayList<HashMap<String, String>>();
It has some IDs and Names like this:

This is the code that is used to loop through:
for (HashMap<String, String> map : mylist)
for (Entry<String, String> entry : map.entrySet())
if(entry.getValue().contains(typedText)){
map1 = new HashMap<String, String>();
map1.put("id", entry.getKey());
map1.put("name", entry.getValue());
mylist1.add(map1);
}
Problem is that in this line:
for (Entry<String, String> entry : map.entrySet())
map.entrySet() shows correct ID and the Name like this:

But only Name is available with 'entry':

entry.getKey() always returns the text 'name' which is the key and
entry.getValue() returns 'Katie Bailey' which is the value.
My question is why am I not getting the key with entry.getKey()? Why do I
always get 'name' as the key with each iteration?

No comments:

Post a Comment