让地图不按字母顺序排序

Getting maps not to sort alphabetically

好的,所以我有一个 Map<String, List<String>> 并且我尝试使用 HashMap(它不按字母顺序排序,也不按照它们的放入方式)和 TreeMap 对其进行排序按字母顺序排列。我不想要那个,我希望它按放入的时间排序,所以说如果我添加 "a",然后添加 "c",然后添加 "b",我希望它是:

"a, c, b" 当我循环键时不是 "a, b, c"

提前感谢您的回答。

使用 java.util.LinkedHashMap.

来自https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html

Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key k is reinserted into a map m if m.put(k, v) is invoked when m.containsKey(k) would return true immediately prior to the invocation.)