Java’s collection framework offers a variety of interfaces and classes for our programming needs. Among these interfaces and classes SortedMap interface is an important option when ordered mapping is required.
What is a SortedMap?
SortedMap is the child interface of Map in Java, providing a collection of key-value pairs where the keys are ordered. Unlike a regular HashMap, A SortedMap maintains a sorted order of its key.
SortedMap Methods
SortedMap defines the following specific methods.
- comparator()
- subMap( Object fromKey, Object toKey )
- headMap(Object toKey)
- tailMap(Object fromKey)
- firstKey()
- lastKey()
- keySet()
- values()
- entrySet()
- size()
- isEmpty()
- containsKey(Object key)
- containsValue(Object value)
- get(Object key)
- put(Object key, Object value)
- remove(Object key)
- putAll(Map m)
- clear()
Implementation of SortedMap
One of the most widely used implementations of the f SortedMap interface is the TreeMap class.