SortedMap interface

In this article, we will discuss SortedMap interface with all its important methods in detail

Key points about SortedMap:

  • SortedMap doesn’t allow duplicate keys
  • Stores Key-Value pairs based on sorting-order
  • Sorting-order could be either natural-ordering or customized-ordering

SortedMap interface:

  • SortedMap interface is a sub-interface of Map interface (i.e.; SortedMap extends Map)
  • To represent a group of key-value pairs as a single unit/entity, where duplicate keys aren’t allowed and keys are stored according to some sorting-order
  • SortedMap allows only unique keys to be inserted
  • SortedMap stores key-value pairs in sorting-order on the basis of keys only, not values
  • SortedMap interface defines 6 specific methods in addition to inherited methods from Map interface
  • Present in java.util package and extends java.util.Map interface

32-SortedMap-interace-in-java Source: Team BenchResources.Net

SortedMap interface methods:

 SortedMap method Description
Object firstKey(); returns 1st key in the invoking Map
Object lastKey(); returns last key in the invoking Map
SortedMap headMap(Object obj); returns SortedMap for those map entries, whose keys are less than specified object
i.e.; from SortedMap’s start to (obj -1)
SortedMap tailMap(Object obj); returns SortedMap for those map entries, whose keys are greater than or equal to specified object

i.e.; from SortedMap’s obj to (end -1)

SortedMap subMap(Object obj1, Object obj2); returns SortedMap containing those map entries, whose keys range is starting from obj1 to obj2-1

i.e.; from stat to end-1; start inclusive & end exclusive (obj1 >= keys < obj2)

Comparator comparator(); returns SortedMap’s comparator object

returns null, if SortedMap uses Natural Sorting

Example to understand SortedMap specific method:

  • SortedMap = [10-Alpha, 20-Beta, 30-Gamma, 40-delta, 50-epsilon, 60-lambda, 70-mu];
  • firstKey() = 10
  • lastKey() = 70
  • headMap(40) =[10-Alpha, 20-Beta, 30-Gamma]
  • tailMap(40) =[40-delta, 50-epsilon, 60-lambda, 70-mu]
  • subMap(20, 70) =[20-Beta, 30-Gamma, 40-delta, 50-epsilon, 60-lambda]
  • comparator() = null; as it uses default natural sorting order

SortedMap example

SortedMapMethods.java

package in.bench.resources.java.collection;

import java.util.SortedMap;
import java.util.TreeMap;

public class SortedMapMethods {

	public static void main(String[] args) {

		// creating SortedMap reference and TreeMap object
		SortedMap<Integer, String> sm = new TreeMap<Integer, String>();

		// adding key-value pairs to SortedMap/TreeMap object
		sm.put(3, "Yahoo");
		sm.put(4, "Amazon");
		sm.put(7, "Twiter");
		sm.put(1, "Google");
		sm.put(5, "Reddit");
		sm.put(6, "LinkedIn");
		sm.put(2, "Facebook");

		System.out.println("Elements will be Sorted "
				+ "acc to Natural ordering:\n");
		System.out.println(sm);

		// SortedMap method operation
		System.out.println("\nFirst element of SortedMap : "
				+ sm.firstKey());
		System.out.println("\nLast element of SortedMap : "
				+ sm.lastKey());
		System.out.println("\nHead Map of 4th position : "
				+ sm.headMap(4));
		System.out.println("\nTail Map of 4th position : "
				+ sm.tailMap(4));
		System.out.println("\nSub Map between 2 & 7 : "
				+ sm.subMap(2,7));
	}
}

Output:

Elements will be Sorted acc to Natural ordering:

{1=Google, 2=Facebook, 3=Yahoo, 4=Amazon, 5=Reddit, 6=LinkedIn, 7=Twiter}

First element of SortedMap : 1

Last element of SortedMap : 7

Head Map of 4th position : {1=Google, 2=Facebook, 3=Yahoo}

Tail Map of 4th position : {4=Amazon, 5=Reddit, 6=LinkedIn, 7=Twiter}

Sub Map between 2 & 7 : {2=Facebook, 3=Yahoo, 4=Amazon, 5=Reddit, 
6=LinkedIn}

Note: All methods of SortedMap is non-synchronized

How to make SortedMap synchronized ?

  • It can be easily converted into synchronized SortedMap
  • Using utility method synchronizedMap(sm); of java.util.Collections class
    Map map = Collections.synchronizedMap(sm);
    

Class or interface that extends/implements SortedMap interface

  • NavigableMap interface extends SortedMap interface
  • TreeMap implements NavigableMap interface

Factors to consider while discussing any collection class

We should consider below factors while discussing any implementation class of collection framework or for that matter Map interface,

  • Underlying data structure
  • Duplicates are allowed or Not
  • Insertion order is maintained or Not
  • Whether NULL insertion is possible or Not
  • If possible, how many NULL values can be inserted
  • Whether collection class provide sorting, by default
  • Is there any way to apply customized sorting
  • Performance, while dealing with retrieval or manipulation (addition/deletion)
  • By default, all methods are synchronized or Not

 

References:

 

Happy Coding !!
Happy Learning !!

NavigableMap interface
WeakHashMap class