How to copy all element of a Treeset to other

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

public class AddAll {

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(” ts =”+ts);

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

/*
* Adds all of the elements in the specified
* collection to this set if they’re not already present
*/

myts.addAll(ts);

System.out.println(“Element of myts “);

for (String str : myts) {
System.out.println(“Element is = “+myts);
}

}
}

Output:

ts =[chandigarh, dehradun, delhi, kolkata, mumbai]

myts = [chandigarh, dehradun, delhi, kolkata, mumbai]

 

Related Posts:

Leave a Reply

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

− 1 = 5