How to convert array to TreeSet

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

public class ArrayToTreeSet {

public static void main(String[] args) {

String [] arr={“java”,”php”,”html”,”json”,”ajax”};

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

Set<String> ts = new TreeSet<String>(Arrays.asList(arr));

// print treeset

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

}

}

Output:

Array element is =java

Array element is =php

Array element is =html

Array element is =json

Array element is =ajax

TreeSet Element = ajax

TreeSet Element = html

TreeSet Element = java

TreeSet Element = json

TreeSet Element = php

 

Related Posts:

 

2 comments

Leave a Reply

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

48 − 46 =