Convert an Array to a Set in Java
This post will discuss how to convert an array to a set using plain Java, Guava, and Apache Commons Collections. Please note that no duplicate elements are allowed in the array, or duplicates will be discarded in the set.
There are three general-purpose set implementations in Java — HashSet, TreeSet, and LinkedHashSet. This post will be using HashSet, which is very fast and offers constant-time operations.
1. Naive solution
A naive solution is to create an empty set and push every element of the specified array into the set.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.util.Set; import java.util.HashSet; class Main { // Generic method to convert an array to a `HashSet` public static <T> Set<T> convertToSet(T[] arr) { // create an empty set Set<T> items = new HashSet<>(); // push each array element into the set for (T i: arr) { items.add(i); } // return the set return items; } // Program to convert an array to a set in Java public static void main(String[] args) { // input array containing all distinct elements String[] str = { "A", "B", "C", "D" }; // convert array to a set Set<String> items = convertToSet(str); // print set System.out.println(items); } } |
Output:
[A, B, C, D]
2. Using Arrays.asList() method
We can pass fixed-size list returned by Arrays.asList() to HashSet constructor to get a mutable set.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.HashSet; import java.util.Arrays; import java.util.Set; class Main { // Program to convert an array to a set in Java public static void main(String[] args) { // input array containing all distinct elements String[] str = { "A", "B", "C", "D" }; // convert array to a set using `Arrays.asList()` method Set<String> items = new HashSet<>(Arrays.asList(str)); // print set System.out.println(items); } } |
Output:
[A, B, C, D]
3. Using Collections.addAll() method
We can use Collections.addAll() provided by Java SE or CollectionUtils.addAll() provided by Apache Commons Collections to add all elements of the specified array to the specified set, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// Generic method to convert an array to a `HashSet` public static <T> Set<T> convertToSet(T[] arr) { // create an empty set Set<T> items = new HashSet<>(); // Adds all elements of the specified array to the specified set Collections.addAll(items, arr); // return the set return items; } |
4. Using Java 8
In Java 8, we can use the Stream to convert an array to a set. The idea is first to convert the specified array to a sequential Stream using Arrays.stream() or Stream.of() and then use a Collector to accumulate the input elements into a new Set.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.Arrays; import java.util.Set; import java.util.stream.Collectors; class Main { // Generic method to convert an array to a set public static <T> Set<T> convertToSet(T[] arr) { return Arrays.stream(arr).collect(Collectors.toSet()); // or // return Stream.of(arr).collect(Collectors.toSet()); } // Program to convert an array to a set in Java public static void main(String[] args) { // input array containing all distinct elements String[] arr = { "A", "B", "C", "D" }; // convert array to a set Set<String> items = convertToSet(arr); // print set System.out.println(items); } } |
Output:
[A, B, C, D]
We can use Collectors.toCollection() to specify the desired Collection as Collectors.toSet() doesn’t guarantee the type of the set returned.
|
1 2 3 4 |
// Generic method to convert an array to a `HashSet` public static <T> Set<T> convertToSet(T[] arr) { return Arrays.stream(arr).collect(Collectors.toCollection(HashSet::new)); } |
5. Using Guava Library
We can also use Guava API to convert an array to a set.
1. Sets.newHashSet() creates a mutable HashSet instance containing the given elements. We should use this method to add or remove elements later, or some of the elements can be null.
|
1 2 3 4 |
// Generic method to convert an array to a `HashSet` public static <T> Set<T> convertToSet(T[] arr) { return Sets.newHashSet(arr); } |
2. We can also use ImmutableSet.copyOf or ImmutableSet.of() that returns an immutable set containing the elements of the specified array. These should be preferred where mutability is not required.
|
1 2 3 4 |
// Generic method to convert an array to a set public static <T> Set<T> convertToSet(T[] arr) { return ImmutableSet.copyOf(arr); } |
That’s all about converting an array to a Set in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)