0

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.

1
  • Just curious, why are you iterating over entrySet() instead of iterating over keySet()? BTW that code works for me. Commented Aug 29, 2013 at 18:26

3 Answers 3

2

You should use an Iterator. Check out the docs

Sign up to request clarification or add additional context in comments.

Comments

1

You need to import the java.util.Map

1 Comment

@Jigglypuff if you are using an IDE, then try to clean ur project.
0

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.