How to Convert a TreeSet to ArrayList

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class TreeSetToList {

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);

List<String> list = new ArrayList<String>(ts);

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

}

}

Output:

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

List Element is = chandigarh

List Element is = dehradun

List Element is = delhi

List Element is = kolkata

List Element is = mumbai

 

Related Posts:

 

Leave a Reply

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

− 1 = 8