gpt4 book ai didi

java - hashmap.keyset() 会按照它们被添加到 hashmap 的顺序返回键吗?

转载 作者:搜寻专家 更新时间:2023-10-31 19:40:56 25 4
gpt4 key购买 nike

我知道 .keySet() 返回一个集合,它是无序的。

据我所知,这意味着我需要保留一个键数组列表,以便跟踪将键添加到 HashMap 中的顺序,对吗?

最佳答案

[...] that means I need to keep an array list of keys in order to track the order in which the keys were added to the hashmap, correct?

是的,没错。或者,您可以使用 LinkedHashMap这是为你做的。

来自文档:

[...] 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). [...]

关于java - hashmap.keyset() 会按照它们被添加到 hashmap 的顺序返回键吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9690525/

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