How To Convert Treeset To Array

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

public class TreeSetToArray {

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(“tree set is =”+ts);

String [] arr = new  String[ts.size()];

/*
* Returns an array containing all of
* the elements in this set
*/

ts.toArray(arr);

System.out.println(“Print Array Element “);

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

}
}

Output:

tree set is =[chandigarh, dehradun, delhi, kolkata, mumbai]

Print Array Element

Array Element is = chandigarh

Array Element is = dehradun

Array Element is = delhi

Array Element is = kolkata

Array Element is = mumbai

Related Posts:

 

Leave a Reply

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

− 2 = 4