TreeSet is a class in Java that implements the SortedSet interface. It stores unique elements in sorted order and internally uses a Red-Black Tree.
Type a number and click Add. Watch how it sorts automatically and rejects duplicates!
// Creating a TreeSet of Integer type
TreeSet<Integer> numbers = new TreeSet<>();
// Demonstration of TreeSet operations
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> set = new TreeSet<>();
set.add(50);
set.add(10);
set.add(30);
set.add(10);
System.out.println(set);
}
}
[10, 30, 50]
Elements are automatically sorted and duplicate values are ignored.