hashmap.keyset()会按照它们添加到hashmap的顺序返回键吗? [英] Will hashmap.keyset() return keys in the order they were added to the hashmap?

查看:886
本文介绍了hashmap.keyset()会按照它们添加到hashmap的顺序返回键吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 .keySet()会返回一个未订购的集合。

I know that the .keySet() returns a set, which is un-ordered.

据我所知,这意味着我需要保留一个数组键列表,以便跟踪键添加到hashmap的顺序,正确?

As far as I can tell, 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?

推荐答案


[...] 这意味着我需要保留一个数组列表为了跟踪键添加到hashmap的顺序,是正确的吗?

是的,那是正确。或者,您可以使用 LinkedHashMap 这是为你做的。

Yes, that's correct. Or, you could use a LinkedHashMap which does this for you.

来自文档:


[...]此实现与HashMap的不同之处在于它维护了一个贯穿其所有条目的双向链表。此链接列表定义迭代排序,通常是将键插入到映射中的顺序(插入顺序)。 [...]

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

这篇关于hashmap.keyset()会按照它们添加到hashmap的顺序返回键吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆