Java – SortedSet interface

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

1. Key points about SortedSet:

  • SortedSet doesn’t allow duplicate items
  • maintains sorting-order

2. 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
18-SortedSet-interace-in-java

Source: Team BenchResources.Net

3. SortedSet interface methods:

 SortedSet methodsDescription
 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

4. 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

SortedSetMethods.java

package in.bench.resources.java.collection;

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");
		System.out.println(ss);

		// 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"));
	}
}

Output:

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

Q) How to make SortedSet synchronized ?

  • SortedSet can be easily converted into synchronized SortedSet
  • Using utility method synchronizedSet(ss); of java.util.Collections class
Set set = Collections.synchronizedSet(ss);

5. Class/interface that extends/implements SortedSet interface

  • NavigableSet interface extends SortedSet interface
  • TreeSet implements NavigableSet interface

6. 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 !!

Java 6 - NavigableSet interface with example
Java - HashSet v/s LinkedHashSet