1.Basic Java program to copy all elements of treeset to an array


  1. package com.TreeSetExamplePrograms;

  2. public Class TreeSetDemo{ 
  3.  
  4. public static void main(String args[]) {
  5. //create TreeSet object
  6.  TreeSet<Integer> treeSet = new TreeSet<Integer>();
  7.        
  8.  //add elements to TreeSet
  9.   treeSet.add(1);
  10.   treeSet.add(3);
  11.   treeSet.add(2);
  12.   treeSet.add(5);
  13.   treeSet.add(6);
  14.   treeSet.add(7);
  15.   treeSet.add(8);
  16.   treeSet.add(9);
  17.   treeSet.add(10);
  18.         
  19.    Object[] objArray = treeSet.toArray();
  20.      //display contents of Object array
  21.  
  22.    System.out.println("TreeSet elements are copied into an Array. Now Array Contains..");
  23.  
  24.   for(int index=0; index < objArray.length ; index++)
  25.       System.out.println(objArray[index]);
  26.          
  27. }
  28. }


Output:


  1. TreeSet elements are copied into an Array. Now Array Contains..
  2. 1
  3. 2
  4. 3
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10

2.Java example program to Convert treeset to integer array

Copy all elements of Java TreeSet to an Object Array

Instance Of Java

We are here to help you learn! Feel free to leave your comments and suggestions in the comment section. If you have any doubts, use the search box on the right to find answers. Thank you! 😊
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu