Switch to full style
Java Collections classes examples .
Post a reply

Convert TreeSet content to array

Mon Oct 22, 2012 8:51 pm

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


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

public class 
ArraysWork {

    public static 
void main(String[] arr) {
        
SortedSet<StringstringsTreeSet = new TreeSet<String>();
        
stringsTreeSet.add("keen");
        
stringsTreeSet.add("adjust");
        
stringsTreeSet.add("zip");
        
stringsTreeSet.add("diagnosis");
        
stringsTreeSet.add("claim");
        
stringsTreeSet.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 : " stringsTreeSet);
       
        
/*
         * Show first element
         */
        
System.out.println("---------------------------------");
        
System.out.println("Element at first:"+stringsTreeSet.first());
        
System.out.println("---------------------------------");
        
/* 
        Copy to array of string...
         */
        
Object[] strArray stringsTreeSet.toArray();
        
/* All elements of array... */
        
for (int i 0strArray.lengthi++) {

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

    }
 


The output is:
Code:
---------------------------------
Elements in Tree set : [adjust, claim, diagnosis, keen, play, zip]
---------------------------------
Element at first:adjust
---------------------------------
Array element : adjust
Array element : claim
Array element : diagnosis
Array element : keen
Array element : play
Array element : zip




Post a reply
  Related Posts  to : Convert TreeSet content to array
 Convert normal array to a list     -  
 Array difference for associate array     -  
 Hide the content of the div     -  
 show the content of set     -  
 compare an array with another array?     -  
 Read the content from directory     -  
 Web content & Website Design     -  
 check folder content using asp     -  
 lowercase table content     -  
 Print the content of hashMap object     -  

Topic Tags

Java Collections