Tech and Media Labs
This site uses cookies to improve the user experience.


Java SortedSet

Jakob Jenkov
Last update: 2019-09-28

The Java SortedSet interface, java.util.SortedSet, is a subtype of the java.util.Set interface. The Java SortedSet interface behaves like a normal Set with the exception that the elements it contains are sorted internally. This means that when you iterate the elements of a SortedSet the elements are iterated in the sorted order.

The TreeSet SortedSet Implementation

The Java Collections API only has one implementation of the Java SortedSet interface - the java.util.TreeSet class. The java.util.concurrent package also has an implementation of this interface, but I will leave the concurrency utilities out of this trail.

Create a TreeSet

Here is an example of creating a Java TreeSet instance:

SortedSet sortedSet = new TreeSet();

Create a TreeSet With a Comparator

It it possible to pass a Comparator, java.util.Comparator implementation to the constructor of the TreeSet. This Comparator will then decide the ordering of the elements in the TreeSet. Here is an example of creating a Java SortedSet with a Comparator:

Comparator comparator = new MyComparatorImpl();

SortedSet sortedSet = new TreeSet(comparator);

Sort Order

The default sort order used by a Java SortedSet is the natural sorting order of the elements. For the SortedSet to be able to determine the natural order of the elements, the elements must implement the java.lang.Comparable interface.

If the elements do not implement the Comparable interface, the elements have no natural ordering. In that case you must pass a Comparator implementation to the SortedSet when you create it. The TreeSet class can take a Comparator in its constructor. Here is an example of creating a Java TreeSet with a Comparator:

Comparator comparator = new MyComparator();

SortedSet sortedSet = new TreeSet(comparator);

Ascending vs. Descending Sort Order

By default the elements of a SortedSet are iterated in ascending order, starting with the "smallest" and moving towards the "largest". But it is also possible to iterate the elements in descending order using the method TreeSet.descendingIterator(). Here is an example of iterating the elements of a TreeSet in descending order:

TreeSet treeSet = new TreeSet();

treeSet.add("one");
treeSet.add("two");
treeSet.add("three");

Iterator iterator = treeSet.descendingIterator();
while(iterator.hasNext()) {
    String element = (String) iterator.next();
    System.out.println(element);
}

Get Comparator Used

If you created your SortedSet with a Comparator, you can obtain that Comparator via the SortedSet comparator() method. Here is an example of obtaining the Comparator used by a SortedSet via the comparator() method:

Comparator comparator = sortedSet.comparator();

Add Elements

You add elements to a Java SortedSet in the same way you do with a normal Java Set - via its add() method. Here is an example of adding an element to a Java SortedSet:

SortedSet sortedSet = new TreeSet();

sortedSet.add("one");

Remove Elements

To remove an element from a SortedSet you call its remove() method, passing the element to remove as parameter. Here is an example of removing an element from a Java SortedSet:

sortedSet.remove("one");

Get First Element

You can get the first element of a SortedSet according to its sort order by calling the first() method of the SortedSet. Here is an example of obtaining the first element from a Java SortedSet according to its sort order:

Object firstElement = sortedSet.first();

Get Last Element

You can get the last element of a SortedSet according to its sort order by calling the last() method of the SortedSet. Here is an example of obtaining the last element from a Java SortedSet according to its sort order:

Object lastElement = sortedSet.last();

Iterate a SortedSet

The way you iterate the elements of a Java SortedSet is the same way you iterate a normal Java Set. You call the SortedSet iterator() method which returns an Iterator, and then you can iterate the elements via that. Here is an example of iterating the elements of a Java SortedSet:

SortedSet sortedSet = new TreeSet();

sortedSet.add("one");
sortedSet.add("two");
sortedSet.add("three");

Iterator iterator = sortedSet.iterator();
while(iterator.hasNext()) {
    String element = (String) iterator.next();
    System.out.println(element);
}

Get Head Set

The Java SortedSet interface has a method named headSet() which returns another SortedSet with all elements that are smaller than (ahead of) a given parameter value, according to the sort order used by the SortedSet. Here is an example of obtaining a head set from a Java SortedSet via its headSet() method:

SortedSet sortedSet = new TreeSet();

sortedSet.add("a");
sortedSet.add("b");
sortedSet.add("c");
sortedSet.add("d");
sortedSet.add("e");

SortedSet headSet = sortedSet.headSet("c");

After running this code the headSet will contain the elements "a" and "b" since these two elements are smaller than (ahead of) the parameter value "c" that was passed to the headSet() method.

Get Tail Set

The Java SortedSet interface has a method named setSet() which returns another SortedSet with all elements that are greater than or equal to (tailing) a given parameter value, according to the sort order used by the SortedSet. Here is an example of obtaining a tail set from a Java SortedSet via its tailSet() method:

SortedSet sortedSet = new TreeSet();

sortedSet.add("a");
sortedSet.add("b");
sortedSet.add("c");
sortedSet.add("d");
sortedSet.add("e");

SortedSet tailSet = sortedSet.tailSet("c");

After running this code the tailSet will contain the elements "c", "d" and "e", since these three elements are greater than or equal to (tailing) the parameter value "c" that was passed to the tailSet() method.

Get Subset

The Java SortedSet interface has a method named subSet() method which will return a new SortedSet which is a subset of the SortedSet the subSet() method is called on. The subSet() method takes two parameter values which specify what elements the returned SortedSet should contain. The returned subset will contain all elements equal to or greater than the first parameter, and smaller than the second parameter, according to the sort order used by the SortedSet. Here is an example of obtaining a subset of a Java SortedSet via its subSet() method:

SortedSet sortedSet = new TreeSet();

sortedSet.add("a");
sortedSet.add("b");
sortedSet.add("c");
sortedSet.add("d");
sortedSet.add("e");

SortedSet subSet = sortedSet.subSet("c", "e");

After running this code the subSet will contain the elements "c" and "d", since bother are equal to or greather than "c" (first parameter), and less than "e" (second parameter).

Generic SortedSet

When you declare a variable of type SortedSet, you can set a generic type on it. For more information about Java generics, see my Java Generics tutorial. Here is an example of setting a generic type on a SortedSet:

SortedSet<String> sortedSet = new TreeSet<>();

This SortedSet can now only contain Java String objects as elements. Among other things, this means that you no longer need to cast the objects obtained from the SortedSet to String. Here is an example of iterating a SortedSet of generic type String:

SortedSet<String> sortedSet = new TreeSet<>();

sortedSet.add("one");
sortedSet.add("two");
sortedSet.add("three");

Iterator iterator = sortedSet.iterator();
while(iterator.hasNext()) {
    String element = iterator.next();
    System.out.println(element);
}

Notice how there is no longer a cast of the object returned from iterator.next() needed. Because the generic type of the SortedSet is String, the compiler knows that the iterator is an Iterato<String>, so next() return String objects.

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC