w00
w00

Reputation: 26762

Get Key value from iterator

I have a HashMap, which contains another HashMap. I want to iterate over the first HashMap and use the Key values from that. Then, as I iterate over the first HashMap I want to start an inner loop iterating over the second HashMap, getting all the values.

The problem I have so far is that I can't figure out how to get the keys from the Iterator.

HashMap<String, HashMap<Integer, String>> subitems = myHashMap.get("mainitem1");

Collection c = subitems.values();
Iterator itr = c.iterator();

while(itr.hasNext())
{
    // Get key somehow? itr.getKey() ???

    // contains the sub items
    HashMap productitem = (HashMap)itr.next();
}

The data that i get from subitems is this:

{Item1{0=sub1, 1=sub2}, Item2{0=sub3, 1=sub4}}

Then, in the while loop productitem contains the 'sub items'. But i can't find out where i can get the key value 'Item1' and 'Item2' from.

How can i get those?

Upvotes: 3

Views: 53254

Answers (5)

Joachim Sauer
Joachim Sauer

Reputation: 308021

You can't get the key from values().iterator().

You need to use entrySet().iterator(). That will return Map.Entry<K,V> objects on which you can call getKey() and getValue().

for (Map.Entry<Integer,Key> entry : subitems.keySet()) {
  Integer key = entry.getKey();
  String value = entry.getValue();

  // do stuff
}

I'd also like to add that having deeply nested maps of lists of maps is usually a sign that you really want to write custom classes to hold your data. Especially when the maps have pre-defined keys to be used and interpretation of the values in the lists depends on the position within the list! I call this code smell "object denial".

Upvotes: 9

Ahmad
Ahmad

Reputation: 2190

java Collections provide facility of EntrySet. This is a list of objects which contain individual keys and values as its properties. You can take a iterator out of this list.

You can get keys as follows.

Iterator i= subitems.entrySet().iterator();

while(i.hasNext()){

String key= i.next().getkey();
}

Upvotes: 2

Francisco Spaeth
Francisco Spaeth

Reputation: 23903

You could do something like this (using iterators):

Set<Entry<String, HashMap<Integer, String>>> c = subitems.entrySet();
Iterator<Entry<String, HashMap<Integer, String>>> iterator = c.iterator();

while(iterator.hasNext())
{
    Entry<String, HashMap<Integer, String>> entry = iterator.next();
    System.out.println("key:" + entry.getKey());
    HashMap<Integer, String> innerMap = entry.getValue();
    if (innerMap == null) {
        continue;
    }
    Iterator<Entry<Integer, String>> innerIterator = innerMap.entrySet().iterator();
    while (innerIterator.hasNext()) {
        Entry<Integer, String> innerEntry = innerIterator.next();
        System.out.println("key:" + innerEntry.getKey() + " value: " + innerEntry.getValue());
    }
}

or like this using foreach structure:

for (Entry<String, HashMap<Integer,String>> entry : subitems.entrySet())
{
    System.out.println("key:" + entry.getKey());
    HashMap<Integer, String> innerMap = entry.getValue();
    if (innerMap == null) {
        continue;
    }
    for (Entry<Integer, String> innerEntry : innerMap.entrySet())
        System.out.println("key:" + innerEntry.getKey() + " value: " + innerEntry.getValue());
    }
}

Upvotes: 3

Aleks G
Aleks G

Reputation: 57316

You can iterate over entries using entrySet().iterator() on the first HashMap or get the keys and iterate over them: Instead of subitems.values().iterator() use subitems.keys().iterator() and use the next key to get the inner hashmap.

Upvotes: 1

aioobe
aioobe

Reputation: 420951

You can't go from value to key in a map. (There may be several keys mapping to the same value!)

You can iterate over the map entries though using subitems.entrySet().iterator(), or you can iterate over the keys, and in each iteration retrieve the associated value through subitems.get(key).

Upvotes: 3

Related Questions