SortedSet interface

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

SortedSet doesn’t allow duplicate items and maintains sorting order


SortedSet interface:

  • SortedSet interface is a sub-interface of Set interface (i.e.; SortedSet extends Set)
  • To represent a group of element/objects as a single unit/entity, where duplicates aren’t allowed and element/objects are stored according to some sorting order
  • SortedSet allows only unique element/objects to be inserted
  • SortedSet stores element/objects in sorting order
  • SortedSet interface defines 6 specific methods in addition to inherited methods from Set/Collection interface
  • Present in java.util package and extends java.util.Set interface


Source: Team BenchResources.Net


SortedSet interface methods:

 SortedSet methods Description
 Object first(); returns 1st element of invoking Set
 Object last(); returns last element of invoking Set
SortedSet headSet(Object obj); returns SortedSet, whose elements are less than specified object obj

i.e.; from SortedSet’s start to (obj -1)

SortedSet tailSet(Object obj); returns SortedSet, whose elements are greater than or equal to specified object obj

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

SortedSet subSet(Object obj1, Object obj2); returns SortedSet, whose elements range is starting from obj1 to obj2-1

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

Comparator comparator(); returns SortedSet’s comparator object

returns null, if SorteSet uses Natural Sorting


Example to understand SortedSet specific methods

SortedSet = [10, 20, 30, 40, 50, 60, 70];

first() = 10
last() = 70
headSet(40) =[10, 20, 30]
tailSet(40) =[40, 50, 60, 70]
subSet(20, 70) =[20, 30, 40, 50, 60]
comparator() = null; as it uses default natural sorting order


SortedSet example


import java.util.SortedSet;
import java.util.TreeSet;

public class SortedSetMethods {

	public static void main(String[] args) {

		// creating SortedSet reference and TreeSet object of type String
		SortedSet<String> ss = new TreeSet<String>();

		// adding elements to SortedSet/TreeSet object
		ss.add("Sundar Pichai");
		ss.add("Satya Nadella");  
		ss.add("Shiv Nadar");  
		ss.add("Shantanu Narayen");
		ss.add("Sundar Pichai"); // adding duplicate element
		ss.add("Francisco D’Souza");

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

		// SortedSet method operation
		System.out.println("\nFirst element of SortedSet : " 
				+ ss.first());
		System.out.println("\nLast element of SortedSet : " 
				+ ss.last());
		System.out.println("\nHead Set  : " 
				+ ss.headSet("Shantanu Narayen"));
		System.out.println("\nTail Set  : " 
				+ ss.tailSet("Shantanu Narayen"));
		System.out.println("\nSub Set  : " 
				+ ss.subSet("Satya Nadella", "Sundar Pichai"));


Elements Sorted acc to Natural ordering:

[Francisco D’Souza, Satya Nadella, Shantanu Narayen, 
Shiv Nadar, Sundar Pichai]

First element of SortedSet : Francisco D’Souza

Last element of SortedSet : Sundar Pichai

Head Set  : [Francisco D’Souza, Satya Nadella]

Tail Set  : [Shantanu Narayen, Shiv Nadar, Sundar Pichai]

Sub Set  : [Satya Nadella, Shantanu Narayen, Shiv Nadar]


Note: All methods of SortedSet is non-synchronized but it can be easily converted into synchronized SortedSet using utility method of java.util.Collections class

Set set = Collections.synchronizedSet(ss);


Class or interface that extends/implements SortedSet interface

  • NavigableSet interface extends SortedSet interface
  • TreeSet implements NavigableSet 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




Happy Coding !!
Happy Learning !!

NavigableSet interface
HashSet v/s LinkedHashSet