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