How To Convert Treeset To Array

In this Article we will see how to convert TreeSet to array. These examples also show how to convert TreeSet to array using toArray method , native method , java8 . toArray() Method in Java is overloaded.

Table of Content                                                                                                       

1.public Object[] toArray()                                                                                                    2.public <T> T[] toArray(T[] a)                                                                                                3. Native Method                                                                                                                  4. Java8 

 How To Convert TreeSet To Array ?

Let’s discuss different ways of convert TreeSet to array in java using toArray() method , Using Java8 stream and Native method .

  1. TreeSet toArray() – convert to object array

Now , We will see how to convert TreeSet to array in java with the help of toArray() method . toArray() method returns an array containing all of the elements in this collection. toArray() method return array of object.

Syntax

public Object[] toArray() {

   }

Note – toArray() Method return array Of Object. Let’s see this by code.

import java.util.TreeSet;

public class TreeSetToArray {
    public static void main(String[] args) {
    	 //Constructs a new, empty set
		   TreeSet<Integer>	treeSet = new TreeSet<Integer>();
		   
		   //add element in it 
		   treeSet.add(10);
		   treeSet.add(20);
		   treeSet.add(30);
		   
		   //display the element 
		   System.out.println("element in hashset ="+ treeSet);

		   /*
		    * Returns an array containing all 
		    * of the elements in this collection
		    */
		   Object[] array = treeSet.toArray();
		   
		   //print object type array
		   for (Object object : array) {
			System.out.println(object);
		  }
   }
}

Output :

element in hashset =[10, 20, 30]
10
20
30

This method return array of object.You need to convert object to int or string manually .Now you are thinking about How to convert TreeSet to int array in java or how to convert TreeSet to Integer array in java or how to convert TreeSet to string array In java.For these we will use second prototype of toArray() method In next approach .

2. TreeSet toArray(T[] a) – convert to Integer array

In this way , We will see how to convert TreeSet To int array Using toArray(T[] a) method.For this we will create a array with size of TreeSet andthen pass as argument in toArray(T []a) method , and this method return array of element of TreeSet.   See more about toArray() method ArrayList to array example . Let’s see this by code.

import java.util.TreeSet;

public class TreeSetToArray2 {
	public static void main(String[] args) {
		//Constructs a new, empty set
		   TreeSet<Integer>	treeSet = new TreeSet<Integer>();
		   
		   //add element in it 
		   treeSet.add(10);
		   treeSet.add(20);
		   treeSet.add(30);
		   
		   //display the element 
		   System.out.println("element in hashset ="+ treeSet);

           Integer [] arr = new Integer[treeSet.size()];
		   
		   arr = treeSet.toArray(arr);
		   
		   //print object type array
		   for (int element : arr) {
			System.out.println(element);
		  }
		   
	}

}

Output :

element in hashset =[10, 20, 30]
10
20
30

In this method we have learned way to convert Set to int array or convert Set to Integer array. We can convert Set to  String in same way.

3. Native Method

In this method we will Iterate TreeSet and one by one element set to array.Let’s see this by example.

import java.util.TreeSet;

public class TreeSetToArray3 {
	
	public static void main(String[] args) {
		   //Constructs a new, empty set
		   TreeSet<String>	treeSet = new TreeSet<String>();
		   
		   //add element in it 
		   treeSet.add("java");
		   treeSet.add("php");
		   treeSet.add("android");
		   
		   //display the element 
		   System.out.println("element in hashset ="+ treeSet);

           String [] arr = new String[treeSet.size()];
		   
		   int i =0;
		   for (String element : treeSet) {
			arr[i++] = element;
		   }
		   
		   //print arary
		   for (String element : arr) {
			System.out.println(element);
		  }
	}

}

Output :

element in hashset =[android, java, php]
android
java
php

4.  Using Java8 Stream

We can Convert Set to array Using java8 Stream as below code.

public class TreeSetToArray {
    public static void main(String[] args) {
    	 //Constructs a new, empty set
		   TreeSet<Integer> treeSet = new TreeSet<Integer>();
		   
		   //add element in it 
		   treeSet.add(10);
		   treeSet.add(20);
		   treeSet.add(30);
		   
		   //display the element 
		   System.out.println("element in hashset ="+ treeSet);

		   Integer[] array = treeSet.stream().toArray(Integer[]::new);

		   		   
		   //print object type array
		   for (Integer element: array) {
			System.out.println(element);
		  }
   }
}

Output :

element in hashset =[10, 20, 30]
10
20
30

In this article we have learned ways of how to convert TreeSet to array or how to convert Set to int array or how to convert TreeSet to Integer array or how to convert TreeSet to String Array . You can learn more about Collection In Java.

Reference :

A Guide To TreeSet                                                                                                              Java Doc – TreeSet

Leave a Reply

Your email address will not be published. Required fields are marked *

22 + = 23