The java.util.SortedMap interface is a subtype of the java.util.Map interface,
Java Collection Framework - SortedMap Interface
The java.util.SortedMap interface is a subtype of the java.util.Map interface,
with the addition that the elements stored in the map are sorted internally.The SortedMap interface extends Map. It ensures that the entries are
maintained in ascending key order.By default the elements are iterated in ascending order, starting with the
"smallest" and moving towards the "largest". But it is also possible to iterate the elements in descending order using the method TreeMap.descendingKeySet().
The Map interface provides operations for:
Range-view: Performs arbitrary range operations on the sorted map.
Endpoints: Returns the first or last key in the sorted map.
Comparator access: Returns the Comparator used to sort the map (if any).
In addition to those methods defined by SortedMap, the SortedMap interface declares the methods summarized as below.
comparator( ) - This method returns the comparator used to order the elements in this SortedMap, or null if this Map uses the natural ordering of its elements.
firstKey( ) - This method returns the first (lowest) key currently in this SortedMap.
headMap(Object toKey) - This method returns a view of the portion of this sorted map whose keys are strictly less than toKey.
lastKey( ) - This method returns the last (highest) element currently in this SortedMap.
subMap(Object fromKey, Object toKey) - This method returns a view of the portion of this sorted map whose keys range from fromKey, inclusive, to toKey, exclusive.
tailMap(Object fromKey) - This method returns a view of the portion of this sorted map whose keys are greater than or equal to fromKey.
Here is the example to show how to implement SortedMap .
Example:-SortedMapDemo,java
package devmanuals.com; import java.util.*; public class SortedMapDemo { public static void main(String[] args) { SortedMap Smp = new TreeMap(); Smp.put(2, "Two"); Smp.put(1, "One"); Smp.put(3, "Three"); Smp.put(4, "Four"); System.out.println("The Sorted Map elements are : "+Smp); } }Output:-
The Sorted Map elements are : {1=One, 2=Two, 3=Three, 4=Four} |
Download This Code.
Here is the Example of methods implementation of SortedMap interface.
[ 0 ] Comments