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
  1. import java.util.*;    
  2. class SortedMapDemo {    
  3.    public static void main(String args[]) {    
  4.       TreeMap tm = new TreeMap();    
  5.       tm.put("abhishek"new Double(3231.34));    
  6.       tm.put("amit"new Double(464.22));    
  7.       tm.put("subham"new Double(45654.00));    
  8.       tm.put("vipendra"new Double(199.22));    
  9.       tm.put("arjun"new Double(-29.08));    
  10.       Set set = tm.entrySet();    
  11.       Iterator i = set.iterator();    
  12.       while(i.hasNext()) {    
  13.          Map.Entry me = (Map.Entry)i.next();    
  14.          System.out.print(me.getKey() + ": ");    
  15.          System.out.println(me.getValue());    
  16.       }    
  17.       System.out.println();    
  18.       double balance = ((Double)tm.get("Zara")).doubleValue();    
  19.       tm.put("Zara"new Double(balance + 1000));    
  20.       System.out.println("abhishek's new balance: " +    
  21.       tm.get("abhishek"));    
  22.    }    
  23. }    
Output
 
cmdoutput.jpg