How to Remove Set From TreeSet

This example shows how to remove Set from TreeSet . This example also shows how to remove set From TreeSet using removeAll() method.I will recommented to you read this how to remove all element from TreeSet using RemoveAll() Method From TreeSet in Java . Let see removeAll() method example for remove Set From TreeSet/Set in Java

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

public class RemoveSet {

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 remove treeset is = "+ts );

Set<String> set = new TreeSet<String>();

set.add("mumbai");
set.add("DELHI");
set.add("PHP");

System.out.println("set is = "+set);

/*
* Removes from this set all of its elements
* that are contained in the specified collection
*/

ts.removeAll(set);

System.out.println("after remove set from treeset ts"+ ts);

}

}

Output:

before remove treeset is = [chandigarh, dehradun, delhi, kolkata, mumbai]

set is = [DELHI, PHP, mumbai]

after remove set from treeset ts[chandigarh, dehradun, delhi, kolkata]
In this post we have discuss removeAll() method example for remove Set from TreeSet. You can remove Set From HashSet using same way. This post is part of Java TreeSet Examples .
References :

Leave a Reply

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

2 + 8 =