Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

how to convert a TreeSet to an Array using collection in java

how to convert a TreeSet to an Array using collection in java

Previous Home Next

 

In this example we shall see how to convert a TreeSet to a linear array.

For creating a TreeSet we have to import  the java.util package in which we have the class java.util.TreeSet which contains the definition and various that can be performed on a TreeSet. In this example we have created a class named CollectionExample in which we have created a TreeSet and added some elements to it.
 In this example for converting a TreeSet to a linear we have used toArray(Anytypearray[] array) method
on the object of the TreeSet. The syntax for using the same method can be shown as,

toArray(Anytypearray[] array)
The above method returns an array containing all of the elements of the TreeSet in the correct order and the  runtime type of the returned array is similar as that of the specified array.

In the given example we have created a TreeSet and added some elements to it by invoking the add() method on the object of the TreeSet i.e ts. We have also created an array of same size as that of the TreeSet by using the size() method on the TreeSet object ts. The array object is used for storing the elements returned by invoking toArray(Anytypearray[] array) method on the TreeSet object ts. Finally we have displayed the elements of the array using the for loop.
The following statements in the given example converts the TreeSet elements to the array elements,
 
Integer[] array=new Integer[ts.size()];   
This statment creates a new integer array of same size as that of the TreeSet.

array=ts.toArray(array);
This statement converts the TreeSet to the array.

 
package r4r.co.in;
import java.util.*;


public class CollectionExample {

/**
* @param args
*/
public static void main(String[] args) throws ClassCastException, NullPointerException, ArrayStoreException
{
// TODO Auto-generated method stub


try
{
TreeSet<Integer> ts=new TreeSet<Integer>(); // creating a TreeSet object ts

ts.add(new Integer(2)); // adding object to the TreeSet
ts.add(4);
ts.add(7);
ts.add(8);
ts.add(1);
ts.add(9);

System.out.println("The objects of the tree set are as follows: ");
System.out.println(ts); // displaying the contents of the TreeSet

Integer[] array=new Integer[ts.size()]; // creating a new Integer Array named array of same size as of TreeSet
array=ts.toArray(array); // converting the TreeSet to the Array using toArray() method
System.out.println("The contents of the created array is as follows: ");
for(int i=0; i<array.length; i++)
{
System.out.println(array[i]); // displaying the contents of the array
}


}

catch(ClassCastException e)
{
System.out.println(e.getMessage());
}
catch(NullPointerException e)
{
System.out.println(e.getLocalizedMessage());
}
catch(ArrayStoreException e) // handles the exception if elements stored in array are not compatible
{
System.out.println(e.getMessage());
}

}


}


The output of the above program is as follows:

The objects of the tree set are as follows:
[1, 2, 4, 7, 8, 9]
The contents of the created array is as follows:
1
2
4
7
8
9

Previous Home Next