How to remove all element from TreeSet

This example shows how to remove all element from TreeSet in Java.This example also shows how to remove all element from TreeSet  using removeAll() method and clear() method in Java.Let’s see how to delete all element from TreeSet In Java.

How to remove all element from TreeSet in Java ?

We will discuss below ways for remove all element from TreeSet In Java.

1. Using removeAll() Method                                                                                                  2. Using Clear() Method

Let’s see these method one by one

  1. Using removeAll() Method

Now we will discuss removeAll() method example of TreeSet. removeAll() method removes from this set all of its elements that are contained in the specified collection.

Syntax :

public boolean removeAll(Collection<?> c){

   }

Parameters – It takes c collection containing elements to be removed from this set as parameter.
Return Values – removeAll() method return true if this set changed as a result of the call
Exception – It throws following exception
UnsupportedOperationException – throws if the removeAll operation is not supported by this set
ClassCastException – throws if the class of an element of this set is incompatible with the given collection (optional)
NullPointerException – throws  if this set contains a null element and the given collection does not permit null elements (optional), or if the specified collection is null

Let’s see removeAll() method example of TreeSet In Java

import java.util.TreeSet;

public class TreeSetRemoveExample {

	public static void main(String[] args) {
		TreeSet<String> treeSet = new TreeSet<String>();
		treeSet.add("mumbai");
		treeSet.add("delhi");
		treeSet.add("kolkata");
		treeSet.add("chandigarh");
		treeSet.add("dehradun");

		// print treeset
		System.out.println(" Before Remove Tree Set is = ="+treeSet);

		treeSet.removeAll(treeSet);
		
		System.out.println(" After Remove Tree Set is = "+treeSet);
		
	}
}

Output :

Before Remove Tree Set is = =[chandigarh, dehradun, delhi, kolkata, mumbai]
 After Remove Tree Set is = []

2. Using Clear() Method

We can remove all element from TreeSet in java using clear() method. clear() method removes all of the elements from this set.

Syntax :

public void clear() {
    
    }

Removes all of the elements from this set. The set will be empty after this call returns.Let’s see clear() method example.

import java.util.Set;
import java.util.TreeSet;

public class RemoveAll {

public static void main(String[] args) {

Set<String> ts = new TreeSet<String>();
ts.add("mumbai");
ts.add("delhi");
ts.add("kolkata");
ts.add("chandigarh");
ts.add("dehradun");

// print treeset
System.out.println(" Before Clear TreeSet is ="+ts);

/*
* Removes all of the elements from  set
*/
ts.clear();

System.out.println(" After Clear Treeset is = "+ts);

}

}

Output:

Before Clear TreeSet is =[chandigarh, dehradun, delhi, kolkata, mumbai]

After Clear Treeset is = []

In this post we have seen removeAll() method example and clear() method example of TreeSet In Java. You can see more good Java TreeSet Examples.

Reference :

A Guide To Java Collection                                                                                                    Java Docs – TreeSet

Leave a Reply

Your email address will not be published. Required fields are marked *

42 − 39 =