Last Updated on 16 June 2019   |   Print Email
In this tutorial, we help you understand deeper about TreeSet in the Java Collections Framework.You know, TreeSet does not only implement the Set interface, it also implements the SortedSet and NavigableSet interfaces. Therefore, besides inheriting behaviors of a typical Set, TreeSet also inherits behaviors of SortedSet and NavigableSet. The following picture illustrates the API hierarchy:
1. Understanding SortedSet
The key characteristic of a SortedSet is that, it sorts elements according to their natural ordering or by a specified comparator. So considering using a TreeSet when you want a collection that satisfies the following conditions:
Duplicate elements are not allowed.
Elements are sorted by their natural ordering (default) or by a specified comparator.
Here’s an example illustrates this characteristic of a SortedSet:
Here, we add elements of an array list to a TreeSet, and as you can see, the duplicate elements are removed and they are sorted by alphanumeric order (natural ordering of numbers).In addition to basic collection operations and normal set operations, the SortedSet provides the following types of operations:
Range view: extracts a portion of the set, i.e. a range.
Endpoints: returns the first and the last element in the sorted set.
Comparator access: returns the comparator, if an, used to sort the set.
Hence the following interface abstracts a SortedSet:
public interface SortedSet<E> extends Set<E> {
// Range-view
SortedSet<E> subSet(E fromElement, E toElement);
SortedSet<E> headSet(E toElement);
SortedSet<E> tailSet(E fromElement);
// Endpoints
E first();
E last();
// Comparator access
Comparator<? super E> comparator();
}
Let’s look at each type of operation in details.
Range view operations:
subSet(E fromElement, E toElement): returns a sorted set which is a portion of the set whose elements range from fromElement, inclusive, to toElement, exclusive.
headSet(E toElement): returns a sorted set which is a portion of the set whose elements are strictly less than toElement.
tailSet(E fromElement): returns a sorted set which is a portion of the set whose elements are greater than or equal to fromElement.
Endpoint operations:
first(): returns the first (lowest) element currently in the set.
last(): returns the last (highest) element currently in the set.
Comparator access:
comparator(): returns the comparator used to order the elements in the set, or null if this set uses the natural ordering of its elements.
2. SortedSet Code Examples
The following code example demonstrates how these operations work with a TreeSetimplementation:
Original Set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
First element: 0
Last element: 9
Sub Set: [3, 4, 5, 6]
Head Set: [0, 1, 2, 3, 4]
Tail Set: [5, 6, 7, 8, 9]
Sorted by natural ordering? true
The following code snippet shows how to use a comparator:
class ZtoAComparator implements Comparator<String> {
public int compare(String s1, String s2) {
return s2.compareTo(s1);
}
}
SortedSet<String> setStrings = new TreeSet<>(new ZtoAComparator());
setStrings.add("Banana");
setStrings.add("Apple");
setStrings.add("Grape");
setStrings.add("Lemon");
setStrings.add("Watermelon");
System.out.println(setStrings);
Output:
[Watermelon, Lemon, Grape, Banana, Apple]
As you see, the specified comparator sorts the elements into descending order.If you use Java 8, use Lambda expression to simplify the comparator class like this:
SortedSet<String> setStrings = new TreeSet<>((s1, s2) -> s2.compareTo(s1));
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments