Array to Set (vice versa)

Java Array to Set

Unlike List, We cannot convert a Java Set into an array directly as it’s NOT implemented using an Array. So We cannot use Arrays class to get the view of array as set. We can follow another approach. We can convert an array into List using Arrays.asList() method, then use it to create a Set. By using this approach, we can covert a Java Array to Set in two ways. Let us discuss them one by one using one simple example.

Approach-1 In this approach, first We need to create a List using given array and use it to create a Set as shown below.

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class ArrayToSet {
	public static void main(String[] args) {

		String[] vowels = { "a", "e", "i", "o", "u" ,"e"};

		Set<String> vowelsSet = new HashSet<String>(Arrays.asList(vowels));
		System.out.println(vowelsSet);

		/**
		 * Unlike List, Set is NOt backed by array, so we can do structural modification
		 * without any issues.
		 */
		vowelsSet.remove("e");
		System.out.println(vowelsSet);
		vowelsSet.clear();
		System.out.println(vowelsSet);
	}
}
[a, e, u, i, o]
[a, u, i, o]
[]

Approach-2 In this approach, we do NOT use intermediate List to create a Set from an Array. First create an empty HashSet, then use Collections.addAll() to copy array elements into the given Set as shown below.

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class ArrayToSet2 {
	public static void main(String[] args) {

		String[] vowels = { "a", "e", "i", "o", "u" };

		Set<String> vowelsSet = new HashSet<>();

		Collections.addAll(vowelsSet, vowels);

		System.out.println(vowelsSet);

		/**
		 * Unlike List, Set is NOt backed by array, so we can do structural modification
		 * without any issues.
		 */
		vowelsSet.remove("e");
		System.out.println(vowelsSet);
		vowelsSet.clear();
		System.out.println(vowelsSet);
	}
}
[a, e, u, i, o]
[a, u, i, o]
[]

Java Set to Array

In this section, we will write a program to convert a Set of Strings into an Array of String using Set.toArray() method as shown below.

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class SetToArray {
	public static void main(String[] args) {
		Set<String> vowelsSet = new HashSet<String>();

		// add example
		vowelsSet.add("a");
		vowelsSet.add("e");
		vowelsSet.add("i");
		vowelsSet.add("o");
		vowelsSet.add("u");

		// convert Set to Array
		String strArray[] = vowelsSet.toArray(new String[vowelsSet.size()]);
		System.out.println(Arrays.toString(strArray));
	}
}
[a, e, u, i, o]

Example :

package certification.collections;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SetFromArray {
	public static void main(String[] args) {

		// Converting list to set
		List<Integer> integers = new ArrayList<>();
		integers.add(2);
		integers.add(2);
		integers.add(3);
		integers.add(4);

		Set<Integer> integers2 = new HashSet<>(integers);
		System.out.println(integers2);

		// Array to list
		Integer[] array = { 2, 3, 45, 4, 2, 4, 5, 2 };

		List<Integer> integers3 = Arrays.asList(array);

		Set<Integer> integers4 = new HashSet<>(integers3);

		System.out.println(integers4);

		// set to array
		Object[] array2 = integers4.toArray();

		for (Object integer : array2) {
			System.out.println(integer);
		}
	}
}
[2, 3, 4]
[2, 3, 4, 5, 45]
2
3
4
5
45

Last updated