Java TreeSet class is a NavigableSet implementation that is based on TreeMap. The elements are either ordered by a Comparator or simply by their natural ordering. In terms of complexity, this implementation provides log(n) time cost for all basic operations like add, remove, contains.
What is important to know about TreeSet in Java
- TreeSet implements the SortedSet interface which means that duplicate values are not allowed.
- Heterogeneous objects cannot be inserted in TreeSet as it does not allow. It will throw classCastException at Runtime.
- TreeSet class does not preserve the insertion order of elements but the elements are sorted by their keys.
- Elements in TreeSet are stored in an ascending and sorted order.
Limitations of TreeSet in Java
- Value must be either Comparable or a Comparator needs to be provided in the constructor
Simple illustration of TreeSet
Constructors in TreeSet
- TreeSet(): constructs a new, empty tree set, sorted according to the natural ordering of its elements
- TreeSet(Collection<? extends E> c): constructs a new tree set that contains the elements in the specified collection and it is sorted according to the natural ordering of its elements.
- TreeSet(Comparator<? super E> comparator): constructs an empty tree set sorted according to the specified comparator.
- TreeSet(SortedSet<E> s): constructs a new tree set exactly the same as the specified set, including the order of the elements.
Methods in TreeSet
- void add(Object o): adds the specified element according to some sorting order in TreeSet. Duplicates won’t be added.
- void clear(): removes all the elements from the tree set.
- boolean contains(Object o): returns true if the given element is present in the tree set, false otherwise.
- Object first(): returns the first element in the tree set.
- Object last(): returns the last element in the tree set.
- boolean isEmpty(): returns true if the tree set contains no elements, returns false otherwise.
- Object clone(): returns a shallow copy of the tree set.
- int size(): returns the number of elements in the tree set.
- Iterator iterator(): returns an iterator for iterating through the elements of the tree set.
- Comparator comparator(): returns Comparator used to sort elements in TreeSet.
For documentation for all the methods, you can visit the Oracle official documentation page.
Adding elements in a TreeSet using add()
Syntax: treeSet.add(Object o)
import java.io.*; import java.util.TreeSet; public class TreeSetExample { public static void main(String args[]) { TreeSet<String> animals = new TreeSet<String>(); animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); System.out.println("TreeSet: " + animals); } }
Output:
TreeSet: [Elephant, Tiger, Lion]
Getting the first and the last element of a TreeSet using first() and last()
Syntax:
treeSet.first()
treeSet.last()
import java.io.*; import java.util.TreeSet; public class TreeSetExample { public static void main(String args[]) { TreeSet<String> animals = new TreeSet<String>(); animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); System.out.println("TreeSet: " + animals); System.out.println("The first element: " + animals.first()); System.out.println("The last element: " + animals.last()); } }
Output:
TreeSet: [Elephant, Tiger, Lion] The first element: Elephant The last element: Lion
Checking whether an element is in a tree set or not using contains()
Syntax: treeSet.contains(Object element)
import java.io.*; import java.util.TreeSet; public class TreeSetExample { public static void main(String args[]) { TreeSet<String> animals = new TreeSet<String>(); animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); System.out.println("TreeSet: " + animals); System.out.println("Is 'Cat' in the tree set?" + animals.contains("Cat")); System.out.println("Is 'Lion' in the tree set?" + animals.contains("Lion")); } }
Output:
TreeSet: [Elephant, Tiger, Lion] Is 'Cat' in the tree set? false Is 'Lion' in the tree set? true
Removing an element from a tree set using remove()
Syntax: treeSet.remove(Object o)
import java.io.*; import java.util.TreeSet; public class TreeSetExample { public static void main(String args[]) { TreeSet<String> animals = new TreeSet<String>(); animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); System.out.println("TreeSet: " + animals); animals.remove("Lion"); animals.remove("Elephant"); System.out.println("TreeSet: " + animals); } }
Output:
TreeSet: [Elephant, Tiger, Lion] TreeSet: [Tiger]
Getting the number of elements in a tree set using size()
Syntax: treeSet.size()
import java.util.*; import java.util.TreeSet; public class TreeSetExample { public static void main(String args[]) { TreeSet<String> animals = new TreeSet<String>(); animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); System.out.println("TreeSet: " + animals); System.out.println("The size of the tree set is: " + animals.size()); } }
Output:
TreeSet: [Elephant, Tiger, Lion] The size of the tree set is: 3
Checking whether a tree set is empty using isEmpty()
Syntax: treeSet.isEmpty()
import java.util.*; import java.util.TreeSet; public class TreeSetExample { public static void main(String args[]) { TreeSet<String> animals = new TreeSet<String>(); animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); System.out.println("TreeSet: " + animals); System.out.println("Empty: " + animals.isEmpty()); animals.clear(); System.out.println("Empty: " + animals.isEmpty()); } }
Output:
TreeSet: [Elephant, Tiger, Lion] Empty: false Empty: true
Iterating through a tree set using Iterator()
Syntax:
Iterator iterator = ts.iterator(); while (iterator.hasNext()) System.out.print(iterator.next());
import java.util.Iterator; import java.util.TreeSet; public class TreeSetExample { public static void main(String[] args) { TreeSet<String> animals = new TreeSet<String>(); animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); Iterator<String> iterator = animals.iterator(); System.out.println("TreeSet: "); System.out.println(); while (iterator.hasNext()) System.out.print(iterator.next() + "\n"); } }
Output:
TreeSet: Elephant Tiger Lion
Iterating through a tree set using enhanced for loop
Syntax:
for (String animal : animals) System.out.print(animal); }
import java.util.Iterator; import java.util.TreeSet; public class TreeSetExample { public static void main(String[] args) { TreeSet<String> animals = new TreeSet<String>(); animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); System.out.println("TreeSet: "); System.out.println(); for (String animal : animals) System.out.print(animal + "\n"); } }
Output:
TreeSet: Elephant Tiger Lion