Java 如何保持插入顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2973751/
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
How to Maintain order of insertion
提问by sarah
I want to add a key, value pair into a hashtable (or any other collection) but have to maintain insertion order. How can I do this?
我想将一个键值对添加到哈希表(或任何其他集合)中,但必须保持插入顺序。我怎样才能做到这一点?
Like I'll add 1 as key "one" as value, 2 as key and "two" as value.
就像我将添加 1 作为键“一”作为值,添加 2 作为键和“二”作为值。
The output should be:
输出应该是:
1:one
2:two
采纳答案by polygenelubricants
Here are the characteristic differences of some important Map
implementations:
以下是一些重要Map
实现的特性差异:
LinkedHashMap
: "with predictable iteration order [...] which is normally the order in which keys were inserted into the map (insertion-order)."HashMap
: "makes no guarantees as to the order of the map"TreeMap
: "is sorted according to the natural ordering of its keys, or by aComparator
"- i.e. it's a
SortedMap
- i.e. it's a
LinkedHashMap
:“具有可预测的迭代顺序 [...],这通常是将键插入映射的顺序(插入顺序)。”HashMap
:“不保证地图的顺序”TreeMap
: "根据其键的自然顺序排序,或按Comparator
"- 即它是一个
SortedMap
- 即它是一个
So it looks like LinkedHashMap
is what you need in this case.
所以LinkedHashMap
在这种情况下,它看起来就是你所需要的。
Here's a snippet to illustrate the differences; it also shows a common way to iterate over all entries of a Map
, and how using an interface to refer to objects allow great flexibility of choice of implementation.
这是一个说明差异的片段;它还展示了迭代 a 的所有条目的常用方法Map
,以及如何使用接口来引用对象允许实现选择的极大灵活性。
import java.util.*;
public class MapExample {
public static void main(String[] args) {
populateThenDump(new HashMap<String,Integer>());
populateThenDump(new TreeMap<String,Integer>());
populateThenDump(new LinkedHashMap<String,Integer>());
}
static void populateThenDump(Map<String,Integer> map) {
System.out.println(map.getClass().getName());
map.put("Zero", 0);
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
map.put("Four", 4);
for (Map.Entry<String,Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " => " + entry.getValue());
}
}
}
The output of the above snippet is (as seen on ideone.com):
上面代码片段的输出是(如在 ideone.com 上看到的):
java.util.HashMap // unordered, results may vary
Three => 3
Zero => 0
One => 1
Four => 4
Two => 2
java.util.TreeMap // ordered by String keys lexicographically
Four => 4
One => 1
Three => 3
Two => 2
Zero => 0
java.util.LinkedHashMap // insertion order
Zero => 0
One => 1
Two => 2
Three => 3
Four => 4
Related questions
相关问题
- Iterate Over Map
- iterating over and removing from a map
- If you want to modify the map while iterating, you'd need to use its
Iterator
.
- If you want to modify the map while iterating, you'd need to use its
Similar questions
类似问题
回答by Peter ?tibrany
For hash table, use LinkedHashMap
class.
对于哈希表,使用LinkedHashMap
类。
回答by Pedantic
You are searching for the LinkedHashMap
class.
您正在寻找LinkedHashMap
班级。