[英]Error while iterating through HashMap in Java
I'm trying to iterate through a HashMap like this: 我试图像这样通过HashMap进行迭代:
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
. 您应该使用
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.