java hashmap 键迭代
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3344734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java hashmap key iteration
提问by ping
Is there any way to iterate through a java Hashmap and print out all the values for every key that is a part of the Hashmap?
有没有办法遍历java Hashmap并打印出作为Hashmap一部分的每个键的所有值?
采纳答案by Esko
With for-each loop, use Map.keySet()
for iterating keys, Map.values()
for iterating values and Map.entrySet()
for iterating key/value pairs.
使用for-each 循环,Map.keySet()
用于迭代键、Map.values()
迭代值和Map.entrySet()
迭代键/值对。
Note that all these are direct views to the map that was used to acquire them so any modification you make to any of the three or the map itself will reflect to all the others too.
请注意,所有这些都是用于获取它们的地图的直接视图,因此您对三者中的任何一个或地图本身所做的任何修改也将反映到所有其他人。
回答by Sanjay Manohar
hashmap.keySet().iterator()
use a for
loop to iterate it.
使用for
循环来迭代它。
then use hashmap.get(item)
to get individual values,
然后用于hashmap.get(item)
获取单个值,
Alternatively just use entrySet()
for getting an iterator for values.
或者只是entrySet()
用于获取值的迭代器。
回答by Jim Garrison
for (Map.Entry<T,U> e : map.entrySet())
{
T key = e.getKey();
U value = e.getValue();
.
.
.
}
In addition, if you use a LinkedHashMap as the implementation, you'll iterate in the order the key/value pairs were inserted. If that's not important, use a HashMap.
此外,如果您使用 LinkedHashMap 作为实现,您将按插入键/值对的顺序进行迭代。如果这不重要,请使用 HashMap。
回答by Jesper
Yes, you do this by getting the entrySet()
of the map. For example:
是的,您可以通过获取entrySet()
地图的 来做到这一点。例如:
Map<String, Object> map = new HashMap<String, Object>();
// ...
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println("key=" + entry.getKey() + ", value=" + entry.getValue());
}
(Ofcourse, replace String
and Object
with the types that your particular Map
has - the code above is just an example).
(当然,替换String
和Object
使用您的特定类型Map
- 上面的代码只是一个例子)。
回答by Fathah Rehman P
public class abcd {
public static void main(String[] args)
{
Map<Integer, String> testMap = new HashMap<Integer, String>();
testMap.put(10, "a");
testMap.put(20, "b");
testMap.put(30, "c");
testMap.put(40, "d");
for (Entry<Integer, String> entry : testMap.entrySet()) {
Integer key=entry.getKey();
String value=entry.getValue();
}
}
}
回答by Alex
Java 8 added Map.forEach
which you can use like this:
添加了 Java 8 Map.forEach
,您可以像这样使用它:
map.forEach((k, v) -> System.out.println("key=" + k + " value=" + v));
There's also replaceAll
if you want to update the values:
还有replaceAll
,如果你要更新的值:
map.replaceAll((k, v) -> {
int newValue = v + 1;
System.out.println("key=" + k + " value=" + v + " new value=" + newValue);
return newValue;
});
回答by Lemmy_Caution
Keep it simple, please:
保持简单,请:
HashMap<String,String> HeyHoLetsGo = new HashMap();
HeyHoLetsGo.put("I", "wanna be your dog");
HeyHoLetsGo.put("Sheena", "is a punk rocker");
HeyHoLetsGo.put("Rudie", "can't fail");
for ( String theKey : HeyHoLetsGo.keySet() ){
System.out.println(theKey + " "+HeyHoLetsGo.get(theKey));
}