How to convert Array to TreeSet

We have read, a lot of things about TreeSet in java. In this post, we will see how to convert array to TreeSet in java. We have couple of ways for convert array to set in java.

Ways to convert array to TreeSet in java

Now we will see different ways for convert array to set in java.

1. Using TreeSet Constructor 

TreeSet Provides a Constructor which can take collection as arguments. With the help of constructor we can add array to set . You can read in details about constructor in TreeSet .

Algorithms :

  1. Create a array .
  2. We will create List from the array with the help of Arrays.asList() method.
  3. This new created list pass as argument in TreeSet constructor
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;

public class ArrayToTreeSet {

public static void main(String[] args) {

String [] arr={"java","php","html","json","ajax"};

for (String str : arr) {
System.out.println("Array element is ="+str);
}

Set<String> ts = new TreeSet<String>(Arrays.asList(arr));

// print treeset

for (String str : ts) {
System.out.println("TreeSet Element = "+str);
}

}

}

Output:

Array element is =java

Array element is =php

Array element is =html

Array element is =json

Array element is =ajax

TreeSet Element = ajax

TreeSet Element = html

TreeSet Element = java

TreeSet Element = json

TreeSet Element = php

2. Using the TreeSet addAll() method

TreeSet provides addAll() method , which can adds all of the elements in the specified collection to this set if they’re not already present (optional operation).

Syntax :

public boolean addAll(Collection<? extends E> c) {

}

Parameters – It Takes as parametric c collection containing elements to be added to this set
Returns Value – It’s true if this set changed as a result of the call
Exception – It’s throws following exceptions
UnsupportedOperationException – Throws if the addAll operation is not supported by this set
ClassCastException – Throws if the class of an element of the given collection prevents it from being added to this set
NullPointerException – if the specified collection contains one or more null elements and this set does not permit null elements, or if the given collection is null
IllegalArgumentException – Throws  if some property of an element of the specified collection prevents it from being added to this set.

Algorithms :

  1. Create array.
  2. Create List from array using Arrays.asList() .
  3. Add new created list in TreeSet with the help of addAll() method.
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;

public class TreeSetAddAll {
	
	public static void main(String[] args) {
		
		String [] arr={"java","php","html","json","ajax"};
		
		Set<String> treeSet = new TreeSet<>();
		
		treeSet.addAll( Arrays.asList(arr));
		
		System.out.println("treeset element are :"+ treeSet);
		}
  }

Output:

treeset element are :[ajax, html, java, json, php]

3. Using Collections addAll() method

We can store array in set java by using Collections addAll() method . Let’s see code example for convert array to set java using collection addAll().

import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

public class TreeSetAddAll {
	
	public static void main(String[] args) {
		
		String [] arr={"java","php","html","json","ajax"};
		
		Set<String> treeSet = new TreeSet<>();
		
		Collections.addAll(treeSet, arr);
		
		System.out.println("treeset element are :"+ treeSet);
		}
  }

Output :

treeset element are :[ajax, html, java, json, php]

4. Using for loop

In this way we will add element in TreeSet one by one.

Algorithms :

  1. Create array and add element in to it.
  2. Create TreeSet.
  3. Iterate array and one by one add element in TreeSet Using add() method.
import java.util.Set;
import java.util.TreeSet;

public class TreeSetAddAll {

	public static void main(String[] args) {

		String[] arr = { "java", "php", "html", "json", "ajax" };

		Set<String> treeSet = new TreeSet<>();
		for (String element : arr) {
			treeSet.add(element);
		}

		System.out.println("treeset element are :" + treeSet);
	}
}

Output :

treeset element are :[ajax, html, java, json, php]

References :

A Guide to TreeSet                                                                                                              Java Doc – TreeSet

Leave a Reply

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

15 − = 14