且构网

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

Hashmap 是否自动排序?

更新时间:2023-12-04 19:30:22

奇怪的是,当我调用 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.