How to Remove Set From TreeSet

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]

Related Posts:

Leave a Reply

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

− 1 = 1