This example shows how to get headset, tailset, and subset from the TreeSet object using the headSet, tailSet, and subSet methods of the TreeSet class in Java.
How to get a headset from TreeSet using the headSet method?
The headSet
method of the TreeSet class returns a Set view of a part of the TreeSet containing elements less than the specified element.
1 |
public SortedSet<E> headSet(E toElement) |
The returned Set will contain all the TreeSet elements that are less than the specified element. The specified element will not be present in the resulting headset.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.Set; import java.util.TreeSet; public class TreeSetSubSetExample { public static void main(String[] args) { TreeSet<Integer> tSetEvenNumbers = new TreeSet<Integer>(); tSetEvenNumbers.add(2); tSetEvenNumbers.add(4); tSetEvenNumbers.add(6); tSetEvenNumbers.add(8); tSetEvenNumbers.add(10); /* * To get a headset, use the headSet method. * * This will return a set containing elements 2 * and 4 i.e. elements < 6. */ Set<Integer> headset = tSetEvenNumbers.headSet(6); System.out.println("Headset contains: " + headset); } } |
Output
1 |
Headset contains: [2, 4] |
As we can see from the output, the element 6 was not included in the headset. If you want to include the specified element in the headset, you can use the overloaded headSet
method that also accepts a boolean parameter.
1 |
public NavigableSet<E> headSet(E toElement, boolean inclusive) |
1 2 3 4 5 6 7 8 9 10 11 |
TreeSet<Integer> tSetEvenNumbers = new TreeSet<Integer>(); tSetEvenNumbers.add(2); tSetEvenNumbers.add(4); tSetEvenNumbers.add(6); tSetEvenNumbers.add(8); tSetEvenNumbers.add(10); Set<Integer> headset = tSetEvenNumbers.headSet(6, true); System.out.println("Headset contains: " + headset); |
Output
1 |
Headset contains: [2, 4, 6] |
How to get a tailset from TreeSet using the tailSet method?
The tailSet
method of the TreeSet class returns a Set view of a part of the TreeSet containing elements greater than or equal to the specified element.
1 |
public SortedSet<E> tailSet(E fromElement) |
The returned Set will contain all the TreeSet elements that are greater than or equal to the specified element.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
TreeSet<Integer> tSetEvenNumbers = new TreeSet<Integer>(); tSetEvenNumbers.add(2); tSetEvenNumbers.add(4); tSetEvenNumbers.add(6); tSetEvenNumbers.add(8); tSetEvenNumbers.add(10); /* * To get a tailset, use the tailSet method. * * This will return a set containing elements 6, 8, * and 10 i.e. elements >= 6. */ Set<Integer> tailset = tSetEvenNumbers.tailSet(6); System.out.println("Tailset contains: " + tailset); |
Output
1 |
Tailset contains: [6, 8, 10] |
If you do not want the specified element to be returned in the tailset, you can use the overloaded tailSet
method that also accepts a boolean parameter.
1 |
public NavigableSet<E> tailSet(E fromElement, boolean inclusive) |
1 2 3 4 5 6 7 8 9 10 11 12 |
TreeSet<Integer> tSetEvenNumbers = new TreeSet<Integer>(); tSetEvenNumbers.add(2); tSetEvenNumbers.add(4); tSetEvenNumbers.add(6); tSetEvenNumbers.add(8); tSetEvenNumbers.add(10); //pass false as second parameter to exclude the element 6 Set<Integer> tailset = tSetEvenNumbers.tailSet(6, false); System.out.println("Tailset contains: " + tailset); |
Output
1 |
Tailset contains: [8, 10] |
How to get a subset from TreeSet using the subSet method?
The subSet
method of the TreeSet class returns a view of part of the TreeSet whose elements range from given start and end elements.
1 |
public SortedSet<E> subSet(E fromElement, E toElement) |
Here, the fromElement is inclusive while the toElement is exclusive.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
TreeSet<Integer> tSetEvenNumbers = new TreeSet<Integer>(); tSetEvenNumbers.add(2); tSetEvenNumbers.add(4); tSetEvenNumbers.add(6); tSetEvenNumbers.add(8); tSetEvenNumbers.add(10); /* * To get a subset, use the subSet method. * * This will return a set containing elements 2, 4, and 6 * i.e. 2 <= elements < 8. */ Set<Integer> subset = tSetEvenNumbers.subSet(2, 8); System.out.println("Subset contains: " + subset); |
Output
1 |
Subset contains: [2, 4, 6] |
If you want to specify the inclusiveness of the from or to elements, you can use the overloaded subSet
method.
1 |
public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) |
This example is a part of the TreeSet in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 TreeSet