The SortedMap interface extends Map. It ensures that the entries is maintained in ascending key order
Several methods throw a nosuchelementexception when no items is in the invoking map. A ClassCastException is thrown if an object was incompatible with the elements in a map. A NullPointerException is thrown if an attempt was made to use a null object when NULL is not allowed in the map.
The methods declared by SortedMap is summarized in the following table:
| SN |
Methods with Description |
| 1 |
Comparator Comparator ()
Returns the invoking sorted map ' s comparator. If The natural ordering is used for the invoking map, and Null is returned. |
| 2 |
Object Firstkey ()
Returns the first key in the invoking map. |
| 3 |
SortedMap Headmap (Object end)
Returns a sorted map for those map entries with keys, is less than end. |
| 4 |
Object Lastkey ()
Returns the last key in the invoking map. |
| 5 |
SortedMap SubMap (object start, Object end)
Returns A map containing those entries with keys that is greater than or equal to start and less than end |
| 6 |
SortedMap Tailmap (Object start)
Returns A map containing those entries with keys that is greater than or equal to start. |
Example:
SortedMap has its implementation in various classes like TREEMAP, following are the example to explain SortedMap functionl Aity:
ImportJava.Util.*;Public Class Treemapdemo { Public Static voidMain(StringArgs[]) { Create a hash map TreeMapTm= New TreeMap(); Put elements to the mapTm.Put("Zara", New Double(3434.34));Tm.Put("Mahnaz", New Double(123.22));Tm.Put("Ayan", New Double(1378.00));Tm.Put("Daisy", New Double(99.22));Tm.Put("Qadir", New Double(-19.08)); Get a set of the entries Set Set =Tm.EntrySet(); Get an iterator IteratorI= Set.Iterator(); Display elements While(I.Hasnext()) { Map.EntryMe= (Map.Entry)I.Next(); System.Out.Print(Me.GetKey() + ": "); System.Out.println(Me.GetValue()); } System.Out.println(); Deposit to Zara ' s account DoubleBalance= ((Double)Tm.Get("Zara")).Doublevalue();Tmput ( "Zara" , new double (balance Span class= "pun" >+ 1000); system.. Println ( "Zara ' s New Balance:" + Tm. Get "Zara" }} /span>
This would produce the following result:
Ayan:1378.0Daisy99.22Mahnaz:123.22Qadir:-19.08Zara :3434.34Zara. s current balance:4434.34
Java-the SortedMap Interface