简体   繁体   中英

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.

You should use an Iterator . Check out the docs

您需要导入java.util.Map

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM