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.
importjava.util.Arrays;importjava.util.HashSet;importjava.util.Set;publicclassArrayToSet {publicstaticvoidmain(String[] args) {String[] vowels = { "a","e","i","o","u","e"};Set<String> vowelsSet =newHashSet<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.
importjava.util.Collections;importjava.util.HashSet;importjava.util.Set;publicclassArrayToSet2 {publicstaticvoidmain(String[] args) {String[] vowels = { "a","e","i","o","u" };Set<String> vowelsSet =newHashSet<>();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.
importjava.util.Arrays;importjava.util.HashSet;importjava.util.Set;publicclassSetToArray {publicstaticvoidmain(String[] args) {Set<String> vowelsSet =newHashSet<String>();// add examplevowelsSet.add("a");vowelsSet.add("e");vowelsSet.add("i");vowelsSet.add("o");vowelsSet.add("u");// convert Set to ArrayString strArray[] =vowelsSet.toArray(newString[vowelsSet.size()]);System.out.println(Arrays.toString(strArray)); }}
[a, e, u, i, o]
Example :
packagecertification.collections;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.HashSet;importjava.util.List;importjava.util.Set;publicclassSetFromArray {publicstaticvoidmain(String[] args) {// Converting list to setList<Integer> integers =newArrayList<>();integers.add(2);integers.add(2);integers.add(3);integers.add(4);Set<Integer> integers2 =newHashSet<>(integers);System.out.println(integers2);// Array to listInteger[] array = { 2,3,45,4,2,4,5,2 };List<Integer> integers3 =Arrays.asList(array);Set<Integer> integers4 =newHashSet<>(integers3);System.out.println(integers4);// set to arrayObject[] array2 =integers4.toArray();for (Object integer : array2) {System.out.println(integer); } }}