SortedMap Interface in Java with Examples
SortedMap is an interface in the collection framework. This interface extends the Map interface and provides a total ordering of its elements (elements can be traversed in sorted order of keys). The exampled class that implements this interface is TreeMap.
The main characteristic of a SortedMap is that it orders the keys by their natural ordering, or by a specified comparator. So consider using a TreeMap when you want a map that satisfies the following criteria:
- null key or null value is not permitted.
- The keys are sorted either by natural ordering or by a specified comparator.
Methods of SortedMap:
- subMap(K from the key, K toKey): Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.
- heatmap(K toKey): Returns a view of the portion of this map whose keys are strictly less than toKey.
- trail map(K from key): Returns a view of the portion of this map whose keys are greater than or equal to from key.
- firstly(): Returns the first (lowest) key currently in this Map.
- Laskey(): Returns the last (highest) key currently in this Map.
- comparator(): Returns the Comparator used to order the keys in this Map, or null if this Map uses the natural ordering of its keys.
- values(): Returns a Collection view of the values contained in this map.
- keySet(): Returns a Set view of the keys contained in this map.
- entrySet(): Returns a Set view of the mappings contained in this map.
Code for SortedMap:
public interface SortedMap extends Map { Comparator comparator(); SortedMap subMap(K fromKey, K toKey); SortedMap headMap(K toKey); SortedMap tailMap(K fromKey); K firstKey(); K lastKey(); }
filter_none
edit
play_arrow
brightness_4
|
Output:
Key : 1 value : geeksforgeeks Key : 2 value : practice Key : 3 value : quiz Key : 4 value : contribute Key : 5 value : code
post a comment