java - 如何在 Java 中将 HashMap 中的键添加到具有相同顺序的列表中

标签 java list sorting hashmap

我使用 HashMap 从键的值中对键进行排序。现在我希望将 key 转移到一个列表中,其中 key 的顺序将相同。我尝试了多种选项将每个键添加到列表中,但它们都没有像 HashMap 中那样保持相同的键顺序。

最佳答案

HashMapMap 最知名和最常见的实现。但是:

This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

如果您想保留元素放入 Map 的顺序,则需要使用 LinkedHashMap执行。正如文档所述:

This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).

关于java - 如何在 Java 中将 HashMap 中的键添加到具有相同顺序的列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51694388/

相关文章:

java - Shift + F6 和右键单击运行之间的区别?

java - 我无法在使用 Net beans 时修改标签中的值

java - 将 "deepToString()"转换回二维数组

python - 为什么 list(set([2,1,3,6,5,3,6,4])) 会自动对列表进行排序?

powershell - 按字母顺序排序,小写在大写之前?

java - 添加命令到另一个插件?

python - 两种方式去除重复的开始和结束节点

python - 更改一个字典值会更改所有值

c - 选择排序 - 我自己的排序版本

algorithm - 冒泡排序性能