Is there a map like data structure with integer as key , string as value, which ends up to be sorted by key no matter on insertion order like when you insert first item {4, somevalue} that it will be on the fourth place when you use it in further application flow
Share
The
interface SortedMap<K,V>defines a type that has the behavior you’re looking for:A particular implementation of a
SortedMapthat may be of interest is aTreeMap:Note that
interface NavigableMap<K,V> extends SortedMap<K,V>. That is, aNavigableMapis aSortedMap, but it also specifies additional methods, e.g. all-inclusive boundssubMap. If you don’t need these additional functionarlities, simply aSortedMapshould work just fine.See also
Example
This shows basic usage of
NavigableSet<K,V>:Related questions
On basic
Mapmanipulation:Others of interest:
On
SortedMapvsNavigableMap: