Question

I have UnsortedMap<String, CustomObject>. Here key, though of String type stores numeric values.

Though there are many ways to sort a Map but i want to do sorting using SortedMap interface.

Is there any way to achieve this. Any help will be appreciated

Was it helpful?

Solution

See SortedMap, specifically All Known Implementing Classes.

Using TreeMap is probably not bad in your case:

SortedMap<String,CustomObject> map = new TreeMap<String,CustomObject>();
map.putAll( yourUnsortedMap );

On a side note, if you know your keys to be numeric, why not have the Map be of erasure <Integer,CustomObject> instead?

Cheers,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top