Jigglypuff
Jigglypuff

Reputation: 1463

Error while iterating through HashMap in Java

I'm trying to iterate through a HashMap like this:

for (Map.Entry<String, Integer> entry : map.entrySet()) {
    String key = entry.getKey();
    Integer value = entry.getValue();
}

but I get this error:

entry cannot be resolved

Is this the wrong way to do it? From what I can tell, this seems to work for others.

Upvotes: 0

Views: 194

Answers (3)

AmirtharajCVijay
AmirtharajCVijay

Reputation: 1108

try this

for(Iterator<Map.Entry<String,Integer>> it = map.entrySet().iterator(); it.hasNext();)
{
    Map.Entry<String,Integer> entry = it.next();
    String key = entry.getKey();
    Integer value = entry.getValue();
}

Upvotes: 0

user1231232141214124
user1231232141214124

Reputation: 1349

You should use an Iterator. Check out the docs

Upvotes: 2

Juned Ahsan
Juned Ahsan

Reputation: 68715

You need to import the java.util.Map

Upvotes: 1

Related Questions