gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:00:12 25 4
gpt4 key购买 nike

我使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com