在 Python 中,您可以在字典中包含键值对,您可以在其中循环遍历它们,如下所示:
for k,v in d.iteritems():
print k,v
有没有办法用 Java HashMap 做到这一点?
最佳答案
是的——例如:
Map<String, String> map = new HashMap<String, String>();
// add entries to the map here
for (Map.Entry<String, String> entry : map.entrySet()) {
String k = entry.getKey();
String v = entry.getValue();
System.out.printf("%s %s\n", k, v);
}
关于java - 像 Python 字典一样循环 Java HashMap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3157558/