This example shows how to remove elements from the TreeSet in Java. This example also shows how to remove the specified element, first element, last element or all elements from the TreeSet object.
How to remove elements from TreeSet in Java?
There are several methods using which we can remove elements from the TreeSet in Java.
1. Removing the specified element
The remove
method removes the specified element from the TreeSet object.
1 |
public boolean remove(Object o) |
The remove
method removes the given element from the TreeSet and returns true if found. If the specified element is not found, the set remains unchanged and it returns false.
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.TreeSet; public class TreeSetRemoveElementsExample { public static void main(String[] args) { TreeSet<Integer> tSetNumbers = new TreeSet<Integer>(); tSetNumbers.add(1); tSetNumbers.add(2); tSetNumbers.add(3); tSetNumbers.add(4); /* * To remove an element from the Treeset, * use the remove method */ //this will remove element 2 and return true System.out.println( tSetNumbers.remove(2) ); //this will return false as the element 5 does not exist System.out.println( tSetNumbers.remove(5) ); System.out.println("TreeSet contains: " + tSetNumbers); } } |
Output
1 2 3 |
true false TreeSet contains: [1, 3, 4] |
If the TreeSet contains the custom class object elements, we can remove the elements in the same way.
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 27 28 29 30 31 32 33 34 35 36 37 38 |
import java.util.TreeSet; class Student implements Comparable<Student>{ private int id; public Student(int id){ this.id = id; } public int getId(){ return this.id; } public String toString(){ return "Student -> " + getId(); } public int compareTo(Student otherStudent) { return this.getId() - otherStudent.getId(); } } public class TreeSetRemoveElementsExample { public static void main(String[] args) { TreeSet<Student> tSetStudents = new TreeSet<Student>(); tSetStudents.add(new Student(1)); tSetStudents.add(new Student(2)); tSetStudents.add(new Student(3)); System.out.println( tSetStudents.remove(new Student(2)) ); System.out.println(tSetStudents); } } |
Output
1 2 |
true [Student -> 1, Student -> 3] |
The TreeSet elements must implement the Comparable interface or a custom comparator must be provided when the TreeSet object is created.
2. Removing the first or last element
The pollFirst
and pollLast
methods remove the first and last elements of the TreeSet respectively.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
TreeSet<Integer> tSetNumbers = new TreeSet<Integer>(); tSetNumbers.add(1); tSetNumbers.add(2); tSetNumbers.add(3); tSetNumbers.add(4); System.out.println("TreeSet contains: " + tSetNumbers); /* * Remove first element using the pollFirst method */ System.out.println( tSetNumbers.pollFirst() + " is removed" ); System.out.println("TreeSet contains: " + tSetNumbers); /* * Remove the last element using the pollLast method */ System.out.println( tSetNumbers.pollLast() + " is removed" ); System.out.println("TreeSet contains: " + tSetNumbers); |
Output
1 2 3 4 5 |
TreeSet contains: [1, 2, 3, 4] 1 is removed TreeSet contains: [2, 3, 4] 4 is removed TreeSet contains: [2, 3] |
Please visit how to get first and last elements from the TreeSet example to know more.
3. Removing all elements
The clear
method of the TreeSet class removes all elements of the TreeSet object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
TreeSet<Integer> tSetNumbers = new TreeSet<Integer>(); tSetNumbers.add(1); tSetNumbers.add(2); tSetNumbers.add(3); tSetNumbers.add(4); System.out.println("TreeSet contains: " + tSetNumbers); /* * To remove all elements of the TreeSet use the clear method */ tSetNumbers.clear(); System.out.println("TreeSet contains: " + tSetNumbers); |
Output
1 2 |
TreeSet contains: [1, 2, 3, 4] TreeSet contains: [] |
Please visit how to clear or remove all elements from the Treeset example and how to check if the TreeSet is empty example to know more.
This example is a part of the Java TreeSet Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 TreeSet