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

answers


your answer

Who Are You?

Have an account already? Login before answering!