Java Array to Set

In this tutorial we will learn about How to convert java array to Set . We will discuss how to convert array to Set in java using constructor , set.addAll() method , Collections.addAll() method and loop over array .Please Don’t use duplicate elements in the array, because duplicates will be discarded in the set.

Convert array to Set In Java

Now we will see different approaches for how to convert array to Set in java.

1. Java Array to Set using TreeSet Constructor

In this example we will create a array of Strings. Now we will initialize TreeSet Using Parameterized Constructor with the array (arr) elements .

import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;

public class ArrayToSet {
	
	public static void main(String[] args) {
		
		//create array of Strings
		String [] arr={"java","vogue","welcomes"};

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

        //initialize Set using array elements 
		Set<String> set = new TreeSet<String>(Arrays.asList(arr));

		// print TreeSet Element 
		System.out.println("Set Elements : "+ set);
	}

}

Output:

Array element is : java
Array element is : vogue
Array element is : welcomes
Set Elements : [java, vogue, welcomes]

2. Convert Array to Set using Set.addAll() method

In this example we will convert array to Set in java using addAll() method of Set . Firstly we created array (arr) of Strings .Now we will create Set , then add array elements to it using Set.addAll() method . Let,s see java program add array element to set in java.

import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;

public class ArrayToSet1 {

	public static void main(String[] args) {

		// create array of Strings
		String[] arr = { "java", "vogue", "welcomes" };

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

		Set<String> set = new TreeSet<String>();

		//adding arr elements to the Set using the addAll() method  
		set.addAll(Arrays.asList(arr));
		
		// print TreeSet Element
		System.out.println("Set Elements : " + set);

	}

}

Output:

Array element is : java
Array element is : vogue
Array element is : welcomes
Set Elements : [java, vogue, welcomes]

3. Java Array to Set using Collections.addAll() method

We can convert array to Set in java using Collections.addAll() method. We created array (arr) of Strings firstly .Now We will create Set , then add array elements to it using Collections.addAll() method .

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

public class ArrayToSet2 {

	public static void main(String[] args) {

		// create array of Strings
		String[] arr = { "java", "vogue", "welcomes" };

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

		Set<String> set = new TreeSet<String>();

		//adding elements of array to Set using Collections.addAll() method
		Collections.addAll(set, arr);
		
		// print TreeSet Element
		System.out.println("Set Elements : " + set);

	}

}

Output:

Array element is : java
Array element is : vogue
Array element is : welcomes
Set Elements : [java, vogue, welcomes]

4. Convert Array to Set Using add() Method

In this approach,we will convert array (arr) to Set in java using for loop . We create array of Strings and add some elements into it. We also created one Set. Now We Iterate array (arr) element Using for-each loop and one by one element of array(arr) will added to Set using add() method . Let,s see java program for this approach.

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

public class ArrayToSet3 {

	public static void main(String[] args) {

		// create array of Strings
		String[] arr = { "java", "vogue", "welcomes" };

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

		Set<String> set = new TreeSet<String>();

		for (String element : arr) {
			//add element from array to Set
			set.add(element);
		}
		
		// print TreeSet Element
		System.out.println("Set Elements : " + set);
	}

}

Output:

Array element is : java
Array element is : vogue
Array element is : welcomes
Set Elements : [java, vogue, welcomes]

In this tutorial we have learned java array to set conversion using different approaches . You can see more java TreeSet Program for practice .

References :

A Guide to TreeSet                                                                                                              Java Doc – TreeSet

Leave a Reply

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

− 1 = 4