Using Stream:

// int[] nums = {1,2,3,4,5}
Set<Integer> set = Arrays.stream(nums).boxed().collect(Collectors.toSet())
Answer from Liam.Nguyen on Stack Overflow
Discussions

java - How to convert a set of Integers to an int[] array? - Stack Overflow
I have no problems converting a Set of strings to a String[] array, but I'm having problems doing so with converting a Set of Integer to an int[] array. How can I convert the Integers to its More on stackoverflow.com
🌐 stackoverflow.com
How can I convert a Java HashSet to a primitive int array? - Stack Overflow
I'm not sure what the original poster would want to do with nulls though. I guess omit them from the resulting array. 2010-10-14T06:53:21.31Z+00:00 ... @davidsheldon Valid remark. You may put null into a Set if the implementation supports it (obviously, yes, but that's what Set's Javadoc ... More on stackoverflow.com
🌐 stackoverflow.com
Cannot convert array to set in java. Getting error
In java generics can't be primitives. Therefore when you do Array.asList(nums) you dont get an ArrayList like you're expecting. You get an ArrayList with one element being nums. Therefore your HashSet is of type which is different to Integer. The easiest way to fix this is to make your input Integer[] nums if possible. More on reddit.com
🌐 r/learnprogramming
5
1
November 9, 2022
set method for an array
Please be more specific about what you are trying to achieve. To change an array element at a given index you can simply use the assignment operator: array[index] = newValue More on reddit.com
🌐 r/javahelp
6
0
November 19, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › java › program-to-convert-array-to-set-in-java
Program to convert Array to Set in Java - GeeksforGeeks
July 11, 2025 - This method will return the immutable set instance but we can make it a mutable set by giving it inside the constructor of HashSet. First, input the array element and declare the set object.
🌐
Programiz
programiz.com › java-programming › examples › convert-array-set
Java Program to Convert Array to Set (HashSet) and Vice-Versa
import java.util.*; public class ... above program, we have an array named array. To convert array to set, we first convert it to a list using asList() as HashSet accepts a list as a constructor....
🌐
Coderanch
coderanch.com › t › 633577 › java › int-Set-Integer-direct-methods
int[] to Set -- no direct methods, right? (Beginning Java forum at Coderanch)
May 12, 2014 - Thanks, Winston. I'll remember that as well. Sorry, I can't quote your post right now. I think for converting Integer to an int, the right way ( at least till Java 7 ) is to get the Integer, do the null check, and then use one of the Integer class methods like intValue.
🌐
Vultr Docs
docs.vultr.com › java › examples › convert-array-to-set-hashset-and-vice-versa
Java Program to Convert Array to Set (HashSet) and Vice-Versa | Vultr Docs
December 19, 2024 - Converting an array to a HashSet in Java includes a straightforward process using the Arrays and Collections utilities. This method ensures all duplicate elements are automatically removed. Start by declaring and initializing an array. Convert ...
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Converting int Array to HashSet - Java Code Geeks
October 4, 2023 - Fig. 2: Converting int[] to HashSet Using Java Streams. Apache Commons Lang is a popular library that provides utility classes for common tasks. You can use the ArrayUtils class to convert an int[] to a HashSet.
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › tutorial › reflect › special › arraySetGet.html
Getting and Setting Arrays and Their Components (The Java™ Tutorials > The Reflection API > Arrays and Enumerated Types)
Array provides methods of the form setFoo() and getFoo() for setting and getting components of any primitive type. For example, the component of an int array may be set with Array.setInt(Object array, int index, int value) and may be retrieved with Array.getInt(Object array, int index).
🌐
GeeksforGeeks
geeksforgeeks.org › java › convert-array-to-hashset-in-java
Convert Array to HashSet in Java - GeeksforGeeks
July 23, 2025 - Sets are classified into two parts sorted set and unsorted set both have advantage and disadvantage. Sorted Set i.e TreeSet sort its unique elements but the time-complexity of TreeSet is O(N log N) but unsorted Sets such as HashSet and LinkedSet do change the order of the elements but the difference between the HashSet and LinkedSet is Random Order of its elements. ... Input : Array: [1, 2, 3, 4, 5, 6] Output: Set: [1, 2, 3, 4, 5, 6] Input : Array: [a, b, c, d] Output: Set: [a, b, c, d]
🌐
GeeksforGeeks
geeksforgeeks.org › java › program-to-convert-set-of-integer-to-array-of-integer-in-java
Program to Convert Set of Integer to Array of Integer in Java - GeeksforGeeks
July 11, 2025 - // Java Program to convert // ... " + setOfInteger); // Convert Set of integers to set of String int[] intArray = convertIntSetToStringSet(setOfInteger); // Print the set of String System.out.println("Array of Integer: " ...
🌐
JavaMadeSoEasy
javamadesoeasy.com › 2015 › 12 › convert-array-to-set-and-set-to-array.html
JavaMadeSoEasy.com (JMSE): convert Array to Set and Set to array in Java
How to convert Array to List and ArrayList to array in Java ... Set does not allows Duplicate elements, so all the duplicate elements were removed. Program/Example 1.2 to convert Integer Array to Set in java >
🌐
Coderanch
coderanch.com › t › 680227 › java › Convert-Set-String-Set-Integer
Convert Set into Set [Solved] (Beginning Java forum at Coderanch)
May 23, 2017 - I want also to sort the set, so I must to convert to array. I use Java 7. ... You can't convert a String instance into an Integer instance by casting -- either implicit or otherwise. This is also true regardless of it being stored in an array or a set. You need to parse the String into an Integer.
🌐
GeeksforGeeks
geeksforgeeks.org › java › set-toarray-method-in-java-with-example
Set toArray() Method in Java - GeeksforGeeks
July 11, 2025 - The Set: [20, 5, 25, 10, 30, 15] The Array is: 20 5 25 10 30 15 · Comment · Article Tags: Article Tags: Java · Java-Collections · Java-Functions · java-set · Java Basics · Introduction to Java3 min read · Java Programming Basics9 min read · Java Methods6 min read ·
🌐
GeeksforGeeks
geeksforgeeks.org › java › set-to-array-in-java
Set to Array in Java - GeeksforGeeks
March 29, 2024 - Input : Set hash_Set = new HashSet(); hash_Set.add("Geeks"); hash_Set.add("For"); Output : String arr[] = {"Geeks", "for"} Method 1 (Simple) We simply create an empty array. We traverse the given set and one by one add elements to the array.
🌐
Instanceofjava
instanceofjava.com › 2018 › 02 › convert-integer-set-to-int-array-java.html
How to Convert integer set to int array using Java 8 - InstanceOfJava
In Java, a Set is a collection that contains no duplicate elements and is unordered. To convert a Set to an array, you can use the toArray() method of the Set interface.
🌐
BeginnersBook -
beginnersbook.com › home › java › convert integer list to int array in java
Convert Integer List to int Array in Java
September 23, 2022 - Print array elements. import java.util.*; public class JavaExample { public static void main(String[] args) { //ArrayList declaration and initialization List<Integer> arrList= Arrays.asList(2, 4, 8, 16); //creating an array of same size int arr[] = new int[arrList.size()]; //iterating ArrayList ...
🌐
YouTube
youtube.com › watch
Java Arrays 1: Set the values in an int array
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
🌐
Studytonight
studytonight.com › java-examples › how-to-convert-array-to-set-in-java
How to Convert Array to Set in Java - Studytonight
January 27, 2021 - This is pretty easy to get set ... } } Apple Orange Banana Orange [Apple, Orange, Banana] We can use asList() method to get convert array to set....