JRishi JRishi - 4 months ago 11
Java Question

sorting object on insertion in hashmap

As per my knowledge of collection framework, if we need to sort object on insertion, we can use

TreeMap
and that object class should override
compareTo()
method of comparable class.

But, I want to know, IS there any way through which we can achieve same in
HashMap
, is there any utility which will enable it to be sorted, while inserting object.

Thanks in advance.

Answer

No, HashMap is not a sorted map by design, it even does not implement SortedMap interface. See HashMap javadocs

Comments