Vote Up 0 Vote Down

Convert TreeSet content to array

Convert the content of ordered Tree set into a normal java array.

Code:

Code:

import java.util.SortedSet; import java.util.TreeSet;

public class ArraysWork {

public static void main(String[] arr) {
    SortedSet<String>  demoTreeSet = new TreeSet<String>();
    demoTreeSet.add("keen");
    demoTreeSet.add("adjust");
    demoTreeSet.add("zip");
    demoTreeSet.add("diagnosis");
    demoTreeSet.add("claim");
    demoTreeSet.add("play");

    // Printing the content of the Tree set
    // Notice that the strings are ordered alphabetically
    System.out.println("---------------------------------");
    System.out.println("Elements in Tree set : " + demoTreeSet);

    /*
     * Show first element
     */
    System.out.println("---------------------------------");
    System.out.println("Element at first:"+ demoTreeSet.first());
    System.out.println("---------------------------------");
    /* 
    Copy to array of string...
     */
    Object[] strArray = demoTreeSet.toArray();
    /* All elements of array... */
    for (int i = 0; i < strArray.length; i++) {

        System.out.println("Array element : " + strArray[i]);
    }

}
edited on 15/12/17

answer


Vote Up 0 Vote Down

Well founded and clear sites for containing all the features of interface that are not easily available in all the websites. People living in different residential areas get benefited with assignmenthelponline.co.uk and also very realistic way to prove themselves.

flag | link

your answer

Who Are You?

Have an account already? Login before answering!