Does Hashmap autosort?

前端 未结 3 1157
有刺的猬
有刺的猬 2021-02-15 14:33

This is my hashmap :

HashMap hashMapTest = new HashMap();

and I insert Date.getTime() into this

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-15 15:38

    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 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.

提交回复
热议问题