Java-the SortedMap Interface

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.