To convert an array to a set in Java, we can make use of the java.util.Arrays
and java.util.HashSet
classes. The Arrays.asList()
method can be used to convert the array to a List, and then the HashSet
constructor can be used to create a Set from that List.
Here is a full Java program that demonstrates this approach:
CrunchifyArrayToSet.java
package crunchify.com.java.tutorials; import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** * @author Crunchify.com * Program: In Java How to convert Arrays to Set? */ public class CrunchifyArrayToSet { public static void main(String[] args) { String[] crunchifyArray = {"Google", "Apple", "Microsoft", "Amazon"}; // Convert the array to a Set Set<String> crunchifySet = new HashSet<>(Arrays.asList(crunchifyArray)); // Print the Set System.out.println("crunchifySet: " + crunchifySet); } }
Console result:
crunchifySet: [Google, Apple, Microsoft, Amazon] Process finished with exit code 0
In this program, we first create an array of strings with some duplicate values. Then, we use the Arrays.asList()
method to convert the array to a List, and pass that List to the HashSet
constructor to create a new Set. Finally, we print out the Set to verify that it contains only unique values.
Note that the generic type of the Set (String
in this case) must match the type of the array that is being converted. Also, because HashSet does not maintain the order of elements, the order of the elements in the Set may be different from the original array.
This is a simplest Way to Convert Arrays to Set in Java with an Integer array.
Java Program: CrunchifyArraysToSet.java
package crunchify.com.tutorials; import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; import java.util.Set; /** * @author Crunchify.com * Program: In Java How to convert Arrays to Set? */ public class CrunchifyArraysToSet { public static void main(String[] args) { Integer[] numbers = {7, 7, 8, 9, 10, 8, 8, 9, 6, 5, 4}; Set<Integer> set = new HashSet<Integer>(Arrays.asList(numbers)); System.out.println("Print Set Value via toString(): " + set.toString()); //if you want to use Iterator to print value System.out.println("\nPrint Set Value via Iterator: "); for (Iterator<Integer> iterator = set.iterator(); iterator.hasNext(); ) { Object o = iterator.next(); System.out.print(o + " "); } } }
Console Result:
Print Set Value via toString(): [4, 5, 6, 7, 8, 9, 10] Print Set Value via Iterator: 4 5 6 7 8 9 10
In this program, we create an Integer array with some duplicate values. We use the same approach as in the previous program to convert the array to a Set. The HashSet
constructor takes a Collection
as an argument, so we use the Arrays.asList()
method to convert the array to a List
before passing it to the constructor. Finally, we print out the Set to verify that it contains only unique values.
Notice how we have used Generics also in above code snippet. Thus if you have an ArrayList
than you can convert it to Set with one simple line of code.