In Java, a SortedMap is a map object that provides an ordering on its keys.
SortedMap is a Java interface for a map that further provides a total ordering on its keys. Quoting the official documentation:
The map is ordered according to the natural ordering of its keys, or by a
Comparator
typically provided at sorted map creation time. This order is reflected when iterating over the sorted map's collection views (returned by the entrySet, keySet and values methods). Several additional operations are provided to take advantage of the ordering. (This interface is the map analogue ofSortedSet
.)All keys inserted into a sorted map must implement the
Comparable
interface (or be accepted by the specified comparator). Furthermore, all such keys must be mutually comparable:k1.compareTo(k2)
(orcomparator.compare(k1, k2)
) must not throw aClassCastException
for any keysk1
andk2
in the sorted map. Attempts to violate this restriction will cause the offending method or constructor invocation to throw aClassCastException
.
Similar SortedMap
interfaces exist in other languages, including Scala.