Java - The SortedMap Interface

來源:互聯網
上載者:User

標籤:

 

The SortedMap interface extends Map. It ensures that the entries are maintained in ascending key order

Several methods throw a NoSuchElementException when no items are in the invoking map. A ClassCastException is thrown when an object is incompatible with the elements in a map. A NullPointerException is thrown if an attempt is made to use a null object when null is not allowed in the map.

The methods declared by SortedMap are 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, 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 that are 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 are greater than or equal to start and less than end

6 SortedMap tailMap(Object start)

Returns a map containing those entries with keys that are greater than or equal to start.

Example:

SortedMap have its implementation in various classes like TreeMap, Following is the example to explain SortedMap functionlaity:

import java.util.*;public class TreeMapDemo {   public static void main(String args[]) {      // Create a hash map      TreeMap tm = new TreeMap();      // Put elements to the map      tm.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      Iterator i = set.iterator();      // Display elements      while(i.hasNext()) {         Map.Entry me = (Map.Entry)i.next();         System.out.print(me.getKey() + ": ");         System.out.println(me.getValue());      }      System.out.println();      // Deposit 1000 into Zara‘s account      double balance = ((Double)tm.get("Zara")).doubleValue();      tm.put("Zara", new Double(balance + 1000));      System.out.println("Zara‘s new balance: " +      tm.get("Zara"));   }}

This would produce the following result:

Ayan: 1378.0Daisy 99.22Mahnaz: 123.22Qadir: -19.08Zara: 3434.34Zara.s current balance: 4434.34

Java - The SortedMap Interface

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.