SortedMap Interface in Java - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Sunday, February 12, 2017

SortedMap Interface in Java

SortedMap Interface

The java.util.SortedMap interface extends the java.util.Map interface 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.

The order of the sorting is either the natural sorting order of the elements (if they implement java.lang.Comparable), or the order determined by a Comparator that you can give to the SortedSet.

Here is the topic that will cover in this post.

  1. SortedMap Interface Declaration
  2. SortedMap Implementations
  3. SortedMap Interface Methods
  4. Example
  5. Exceptions

SortedMap Interface Declaration

This interface is a member of java the java Collection framework. It is available since Java 1.2. SortedMap is a generic interface and declared as show here:

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

Here, K specifies the type of keys, and V specifies the type of values.

SortedMap Implementations

The Java Collections framework only has one implementation of the SortedMap interface - the java.util.TreeMap class. The java.util.concurrent package also has an implementation of this interface.

SortedMap sortedMap = new TreeMap();

Comparator comparator = new MyComparator();
SortedMap treeMap = new TreeMap(comparator);

SortedMap Interface Methods

Method Description
Comparator<? super K> comparator() Returns the comparator used to order the keys in this map, or null if this map uses the Comparable natural ordering of its keys.
SortedMap<K,V> subMap(K fromKey, K toKey) Returns a view of the portion of this map whose keys that are greater than or equal to fromKey and 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.
SortedMap<K,V> headMap(K toKey) 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.
SortedMap<K,V> tailMap(K fromKey); 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.
K firstKey() Returns the first (lowest) key currently in this map.
K lastKey() Returns the last (highest) key currently in this map.
Set<K> keySet() Returns a Set view of the keys contained in this map. The set's iterator returns the keys in ascending order. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.
Collection<V> values() Returns a Collection view of the values contained in this map. The collection's iterator returns the values in ascending order of the corresponding keys. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa.
Set<Map.Entry<K, V>> entrySet() Returns a Set view of the mappings contained in this map. The set's iterator returns the entries in ascending key order. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.

Example

SortedMap interface has implemented in TreeMap class. Following is the example to explain SortedMap.

package com.walking.techie;

import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

public class TreeMapDemo {

  public static void main(String[] args) {
    //create a sorted nap have reference of TreeMap
    SortedMap<String, Double> mobilesPrice = new TreeMap<>();
    mobilesPrice.put("SAMSUNG", new Double(10000.00));
    mobilesPrice.put("MI REDMI NOTE 3", new Double("12000.00"));
    mobilesPrice.put("MI REDMI 3S Prime", new Double("8999.99"));
    mobilesPrice.put("MOTO G", new Double("14000.00"));

    //get entry set of mobilesPrice
    Set<Entry<String, Double>> entries = mobilesPrice.entrySet();

    //get an iterator on entry set
    Iterator<Entry<String, Double>> iterator = entries.iterator();

    System.out.println("Mobile Phone      and   price ");
    while (iterator.hasNext()) {
      Entry<String, Double> mobile = iterator.next();
      System.out.println(mobile.getKey() + "      " + mobile.getValue());
    }
  }
}

Output of above program is shown below:

Mobile Phone      and   price
MI REDMI 3S Prime      8999.99
MI REDMI NOTE 3      12000.0
MOTO G      14000.0
SAMSUNG      10000.0

Exceptions

The SortedMap interface has many methods, and several methods throw below exceptions.

Exception Description
ClassCastException throws when an object is incompatible with the elements in a map.
NullPointerException thrown if an attempt is made to use a null object and null is not allowed in the map.
NoSuchElementException thrown when no items are in the invoking map.
IllegalArgumentException thrown if an invalid argument is used.

No comments :

Post a Comment