To convert array to set , we first convert it to a list using asList() as HashSet accepts a list as a constructor. Then, we initialize the set with the elements of the converted list.
Array. set() is an inbuilt method in Java and is used to set a specified value to a specified index of a given object array. Syntax Array.set(Object []array, int index, Object value)
In this, we can convert the list items to set by using addAll() method. For this, we have to import the package java. util.
Method 1- Using set() to convert list to set Python. The simplest way to convert list to set in Python is by using the set() function. The set() method is used to convert an iterable element such as a list, dictionary, or tuple into the set.
Like this:
Set<T> mySet = new HashSet<>(Arrays.asList(someArray));
In Java 9+, if unmodifiable set is ok:
Set<T> mySet = Set.of(someArray);
In Java 10+, the generic type parameter can be inferred from the arrays component type:
var mySet = Set.of(someArray);
Be careful
Set.of throws IllegalArgumentException - if there are any duplicate elements in someArray. See more details: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Set.html#of(E...)
Set<T> mySet = new HashSet<T>();
Collections.addAll(mySet, myArray);
That's Collections.addAll(java.util.Collection, T...) from JDK 6.
Additionally: what if our array is full of primitives?
For JDK < 8, I would just write the obvious for
loop to do the wrap and add-to-set in one pass.
For JDK >= 8, an attractive option is something like:
Arrays.stream(intArray).boxed().collect(Collectors.toSet());
With Guava you can do:
T[] array = ...
Set<T> set = Sets.newHashSet(array);
Java 8:
String[] strArray = {"eins", "zwei", "drei", "vier"};
Set<String> strSet = Arrays.stream(strArray).collect(Collectors.toSet());
System.out.println(strSet);
// [eins, vier, zwei, drei]
Varargs will work too!
Stream.of(T... values).collect(Collectors.toSet());
We have the option of using Stream
as well. We can get stream in various ways:
Set<String> set = Stream.of("A", "B", "C", "D").collect(Collectors.toCollection(HashSet::new));
System.out.println(set);
String[] stringArray = {"A", "B", "C", "D"};
Set<String> strSet1 = Arrays.stream(stringArray).collect(Collectors.toSet());
System.out.println(strSet1);
// if you need HashSet then use below option.
Set<String> strSet2 = Arrays.stream(stringArray).collect(Collectors.toCollection(HashSet::new));
System.out.println(strSet2);
The source code of Collectors.toSet()
shows that elements are added one by one to a HashSet
but specification does not guarantee it will be a HashSet
.
"There are no guarantees on the type, mutability, serializability, or thread-safety of the Set returned."
So it is better to use the later option. The output is:
[A, B, C, D]
[A, B, C, D]
[A, B, C, D]
Java 9 introduced Set.of
static factory method which returns immutable set for the provided elements or the array.
@SafeVarargs
static <E> Set<E> of(E... elements)
Check Immutable Set Static Factory Methods for details.
We can also get an immutable set in two ways:
Set.copyOf(Arrays.asList(array))
Arrays.stream(array).collect(Collectors.toUnmodifiableList());
The method Collectors.toUnmodifiableList()
internally makes use of Set.of
introduced in Java 9. Also check this answer of mine for more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With