且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Hashmap是否自动排序?

更新时间:2023-12-04 19:34:28


奇怪的是,当我调用hashmap时,顺序完全是
,而键不适合插入:

The strange thing is when i call the hashmap with, the order is completly an other and the keys doesnt fit to the insertion :

HashMap 不维护插入顺序,但还有一个替代方法,称为 LinkedHashMap 保持插入顺序。或者,如果您希望按自然顺序对键进行排序(使用键compareTo方法),则可以使用 TreeMap

HashMap does NOT maintain the order of insertion but there is an alternative called LinkedHashMap that maintains the insertion order. Or if you want the keys to be sorted in natural order(using keys compareTo method) then you may go for TreeMap.