JRishi JRishi - 1 year ago 44
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 Source

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