Saturday 6 June 2015

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: 


No comments:

Post a Comment