Pergunta

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

Foi útil?

Solução

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,

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top