ahairshi
ahairshi

Reputation: 381

Iterate through Hashmap using Entryset

for (Map.Entry<String, Map<String, List>> entry:Map1.entrySet()) 
{
    String key=entry.getKey();
    System.out.println("Type : " +key);

    for (Map.Entry<String, List> entry1 : entry.getKey().getValue().entrySet())
    {
        System.out.println("Type : " + entry1.getKey());
    }
}

I don't know what should be used in place of entry.getKey().getValue().entrySet(). Can any one explains me to understand this. This is for iterating nested map.

the error I got was

.\common\devtracker\process\devtr\DevTrackerImpl.java:226: cannot find symbol
symbol  : method getValue()
location: class java.lang.String
for (Map.Entry<String, List<ProjectBreakupVO>>    entry1:entry.getKey().getValue().entrySet())

Upvotes: 4

Views: 8043

Answers (3)

Indu Devanath
Indu Devanath

Reputation: 2188

If you are trying to iterate through map of type Map<String, List>, and you are facing findbugs issues when you used myMap.keySet() like so:

    for (String keyValue : myMap.keySet()) {
        String key = keyValue;
        List objValue = myMap.get(key);
    }

then try iterating through the map using myMap.entrySet() which is more recommended:

    for(Map.Entry<String, List> entry: myMap.entrySet()) {
        String key = entry.getKey();
        List objValue = entry.getValue();
    }

So nesting forloop in this case would be like:

for (Map.Entry<String, Map<String, List>> entry:Map1.entrySet()) 
{
    String key=entry.getKey();
    System.out.println("Type : " +key);

    for (Map.Entry<String, List> entry1 : entry.getValue().entrySet())
    {
        System.out.println("Type : " + entry1.getKey());
    }
}

Upvotes: 0

Bharat Sinha
Bharat Sinha

Reputation: 14363

You should be using....

for (Map.Entry<String, List> entry1 : entry.getKey().getValue().entrySet())

to fetch the entries for the inner loop.

Upvotes: 0

Ren
Ren

Reputation: 3455

entry.getKey() does not have the method getValue(), as its just returning a string. What you probably want here

for (Map.Entry<String, List> entry1 : entry.getKey().getValue().entrySet())

is instead to do

for (Map.Entry<String, List> entry1 : entry.getValue().entrySet())

Upvotes: 2

Related Questions