Working With SortedMap Interface in Java

Working with SortedMap Interface

In this article we describe one of the important parts of a collection; sorted map. The SortedMap interface extends Map. It ensures that the entries are maintained in ascending key order. A SortedMap is a Map that maintains its entries in ascending order, sorted according to the keys' natural ordering, or according to a Comparator provided at the time of the SortedMap creation. Natural ordering and Comparators are discussed in the Object Ordering section. The SortedMap interface provides operations for normal Map operations and for the following:

  • Range view -performs arbitrary range operations on the sorted map.
  • Endpoints - returns the first or the last key in the sorted map.
  • Comparator access - returns the Comparator, if any, used to sort the map.

650px-Java_collection_set_implementations1.jpg

Syntax

public interface SortedMap<K,V> extends Map<K,V>

Note: Several methods return submaps with restricted key ranges. Such ranges are half-open, that is, they include their low endpoint but not their high endpoint (where applicable). If you need a closed range (which includes both endpoints), and the key type allows for calculation of the successor of a given key, merely request the subrange from lowEndpoint to successor (highEndpoint). For example, suppose that m is a map whose keys are strings. The following idiom obtains a view containing all of the key-value mappings in m whose keys are between low and high, inclusive.

Method Details

1- comparator( ) :
This method returns the invoking sorted map's comparator. If the natural ordering is used for the invoking map, null is returned.

Syntax

Comparator<? super Object> comparator()

2- firstKey( ) :

This method returns the first key in the invoking map. And this method throws a null pointer Exception.

Syntax

Object firstKey()

3- headMap(Object end) :

This method returns a view of the portion of this map whose keys are strictly less than
toKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

Syntax

SortedMap<Object,V> headMap(K toKey)

4- lastKey( )

 This method returns the last key in the invoking map.

Syntax

Object lastKey()

5- subMap

Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive. (If fromKey and toKey are equal, the returned map is empty.) The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

Syntax

SortedMap<K,V> subMap(K fromKey,K toKey)

6- tailMap :

Returns a view of the portion of this map whose keys are greater than or equal to fromKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

Syntax

SortedMap<K,V> tailMap(K fromKey)

Example

import java.util.*;

class SortedMapDemo {

   public static void main(String args[]) {

      TreeMap tm = new TreeMap();

      tm.put("abhishek", new Double(3231.34));

      tm.put("amit", new Double(464.22));

      tm.put("subham", new Double(45654.00));

      tm.put("vipendra", new Double(199.22));

      tm.put("arjun", new Double(-29.08));

      Set set = tm.entrySet();

      Iterator i = set.iterator();

      while(i.hasNext()) {

         Map.Entry me = (Map.Entry)i.next();

         System.out.print(me.getKey() + ": ");

         System.out.println(me.getValue());

      }

      System.out.println();

      double balance = ((Double)tm.get("Zara")).doubleValue();

      tm.put("Zara", new Double(balance + 1000));

      System.out.println("abhishek's new balance: " +

      tm.get("abhishek"));

   }

}

Output

cmdoutput.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all