java 使用 HashMap 时,是否保证在迭代时值和键的顺序相同?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/128008/
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
When using a HashMap are values and keys guaranteed to be in the same order when iterating?
提问by Allain Lalonde
When I iterate over the values or keys are they going to correlate? Will the second key map to the second value?
当我迭代值或键时,它们会相关吗?第二个键会映射到第二个值吗?
回答by Alex Miller
No, not necessarily. You should really use the entrySet().iterator() for this purpose. With this iterator, you will be walking through all Map.Entry objects in the Map and can access each key and associated value.
不,不一定。为此,您真的应该使用 entrySet().iterator() 。使用此迭代器,您将遍历 Map 中的所有 Map.Entry 对象,并可以访问每个键和关联的值。
回答by Matt
to use the entrySet that @Cuchullain mentioned:
使用@Cuchullain 提到的 entrySet:
Map<String, String> map = new HashMap<String, String>();
// populate hashmap
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
// your code here
}
回答by basszero
You want to use this, LinkedHashMap, for predicable iteration order
您想使用这个LinkedHashMap来进行可预测的迭代顺序
回答by Adam Al-Salman
public class Test {
public static void main(String[] args) {
HashMap <String,String> hashmap = new HashMap<String,String>();
hashmap.put("one", "1");
hashmap.put("two", "2");
hashmap.put("three", "3");
hashmap.put("four", "4");
hashmap.put("five", "5");
hashmap.put("six", "6");
Iterator <String> keyIterator = hashmap.keySet().iterator();
Iterator <String> valueIterator = hashmap.values().iterator();
while(keyIterator.hasNext()) {
System.out.println("key: "+keyIterator.next());
}
while(valueIterator.hasNext()) {
System.out.println("value: "+valueIterator.next());
}
}
}
key: two
key: five
key: one
key: three
key: four
key: six
value: 2
value: 5
value: 1
value: 3
value: 4
value: 6
回答by Adam Al-Salman
Both values() and keySet() delegate to the entrySet() iterator so they will be returned in the same order. But like Alex says it is much better to use the entrySet() iterator directly.
values() 和 keySet() 都委托给 entrySet() 迭代器,因此它们将以相同的顺序返回。但正如 Alex 所说,直接使用 entrySet() 迭代器要好得多。
回答by Adam Al-Salman
I agree with pmac72. Don't assume that you'll get ordered values or keys from an unordered collection. If it works time to time it is just pure hazard. If you want order to be preserved, use a LinkedHashMap or a TreeMap or commons collections OrderedMap.
我同意 pmac72。不要假设您将从无序集合中获得有序值或键。如果它不时起作用,那只是纯粹的危险。如果您希望保留顺序,请使用 LinkedHashMap 或 TreeMap 或公共集合 OrderedMap。
回答by Jorge Ferreira
The question confused me at first but @Matt cleared it up for me.
这个问题起初让我感到困惑,但@Matt 为我解决了这个问题。
Consider using the entrySet() method that returns a set with the key-value pairs on the Map.
考虑使用 entrySet() 方法,该方法返回带有 Map 上键值对的集合。
Map<Integer, Integer> a = new HashMap<Integer, Integer>(2);
a.put(1, 2);
a.put(2, 3);
for (Map.Entry<Integer, Integer> entry : a.entrySet()) {
System.out.println(entry.getKey() + " => " + entry.getValue());
}
This outputs:
这输出:
1 => 2
2 => 3
3 => 3
回答by oreoshake
I second @basszero. While
我第二次@basszero。尽管
for (Map.Entry<Integer, Integer> entry : a.entrySet())
will work, I find using a data structure that does this automatically is nicer. Now, you can just iterate "normally"
会起作用,我发现使用自动执行此操作的数据结构更好。现在,您可以“正常”迭代
回答by pmac72
HashMap's keySet method returns a Set, which does not guarantee order.
HashMap's values() method returns a Collection, which does not guarantee order.
HashMap 的 keySet 方法返回一个 Set,不保证顺序。
HashMap 的 values() 方法返回一个 Collection,不保证顺序。
That said, the question was "are they going to correlate" so technically the answer is maybe, but don't rely on it.
也就是说,问题是“它们会相关吗”,所以从技术上讲,答案是可能的,但不要依赖它。

