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
🌐
GeeksforGeeks
geeksforgeeks.org › java › program-to-convert-array-to-set-in-java
Program to convert Array to Set in Java - GeeksforGeeks
July 11, 2025 - The Java Set does not provide control over the position of insertion or deletion of elements. Basically, Set is implemented by HashSet, LinkedHashSet or TreeSet (sorted representation). Set has various methods to add, remove clear, size, etc to enhance the usage of this interface. ... Input: Array: [Geeks, forGeeks, A computer Portal] Output: Set: [Geeks, forGeeks, A computer Portal] Input: Array: [1, 2, 3, 4, 5] Output: Set: [1, 2, 3, 4, 5]
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-an-array-to-set-and-vice-versa-in-java
How to convert an array to Set and vice versa in Java?
import java.util.Arrays; import ... System.out.println("Enter the element "+(i+1)+" (String) :: "); myArray[i]=sc.next(); } Set<String> set = new HashSet<>(Arrays.asList(myArray)); System.out.println("Given array is converted to a Set"); System.out.println("Contents of set ...
🌐
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).
🌐
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 ArraySet { public static void main(String[] args) { String[] array = {"a", "b", "c"}; Set<String> set = new HashSet<>(Arrays.stream(array).collect(Collectors.toSet())); System.out.println("Set: " + set); } } The output of the program is the same as Example 1.
Top answer
1 of 16
3263

You can either use array declaration or array literal (but only when you declare and affect the variable right away, array literals cannot be used for re-assigning an array).

For primitive types:

int[] myIntArray = new int[3]; // each element of the array is initialised to 0
int[] myIntArray = {1, 2, 3};
int[] myIntArray = new int[]{1, 2, 3};

// Since Java 8. Doc of IntStream: https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html

int [] myIntArray = IntStream.range(0, 100).toArray(); // From 0 to 99
int [] myIntArray = IntStream.rangeClosed(0, 100).toArray(); // From 0 to 100
int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).toArray(); // The order is preserved.
int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).sorted().toArray(); // Sort 

For classes, for example String, it's the same:

String[] myStringArray = new String[3]; // each element is initialised to null
String[] myStringArray = {"a", "b", "c"};
String[] myStringArray = new String[]{"a", "b", "c"};

The third way of initializing is useful when you declare an array first and then initialize it, pass an array as a function argument, or return an array. The explicit type is required.

String[] myStringArray;
myStringArray = new String[]{"a", "b", "c"};
2 of 16
336

There are two types of array.

One Dimensional Array

Syntax for default values:

int[] num = new int[5];

Or (less preferred)

int num[] = new int[5];

Syntax with values given (variable/field initialization):

int[] num = {1,2,3,4,5};

Or (less preferred)

int num[] = {1, 2, 3, 4, 5};

Note: For convenience int[] num is preferable because it clearly tells that you are talking here about array. Otherwise no difference. Not at all.

Multidimensional array

Declaration

int[][] num = new int[5][2];

Or

int num[][] = new int[5][2];

Or

int[] num[] = new int[5][2];

Initialization

 num[0][0]=1;
 num[0][1]=2;
 num[1][0]=1;
 num[1][1]=2;
 num[2][0]=1;
 num[2][1]=2;
 num[3][0]=1;
 num[3][1]=2;
 num[4][0]=1;
 num[4][1]=2;

Or

 int[][] num={ {1,2}, {1,2}, {1,2}, {1,2}, {1,2} };

Ragged Array (or Non-rectangular Array)

 int[][] num = new int[5][];
 num[0] = new int[1];
 num[1] = new int[5];
 num[2] = new int[2];
 num[3] = new int[3];

So here we are defining columns explicitly.
Another Way:

int[][] num={ {1}, {1,2}, {1,2,3,4,5}, {1,2}, {1,2,3} };

For Accessing:

for (int i=0; i<(num.length); i++ ) {
    for (int j=0;j<num[i].length;j++)
        System.out.println(num[i][j]);
}

Alternatively:

for (int[] a : num) {
  for (int i : a) {
    System.out.println(i);
  }
}

Ragged arrays are multidimensional arrays.
For explanation see multidimensional array detail at the official java tutorials

🌐
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 >
Find elsewhere
🌐
Coderanch
coderanch.com › t › 633577 › java › int-Set-Integer-direct-methods
int[] to Set -- no direct methods, right? (Beginning Java forum at Coderanch)
Well, one that springs to mind would be to have your method return an Integer[] (or - possibly even better - a List<Integer>). Then you can use Arrays.asList() (or nothing at all). Personally, I rarely use primitive arrays these days, unless I know I'm going to be doing a lot of modification on it; and I certainly try to avoid returning them from public APIs, for exactly the reasons you're running into.
🌐
Baeldung
baeldung.com › home › java › java collections › converting between an array and a set in java
Converting between an Array and a Set in Java | Baeldung
June 9, 2025 - How to Convert between an Array and a Set Using Plain Java, Guava or Apache Commons Collections.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Converting int Array to HashSet - Java Code Geeks
October 4, 2023 - Learn efficiently converting an int array into a HashSet in Java using different methods, including loops, Java Streams, and more!
🌐
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 - Conversely, converting a set to an array could be necessary for operations that are possible only on arrays or for API compatibility. In this article, you will learn how to convert an array into a HashSet, and then back from a HashSet to an array. Discover how using the standard Java Collections ...
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-integer-set-to-int-array-using-java
How to convert integer set to int array using Java?
Using Java8: Since Java8 Streams ... System.out.println(hashSet); //Creating an empty integer array Integer[] array = hashSet.stream().toArray(Integer[]::new); System.out.println(Arrays.toString(array)); } }...
🌐
Sentry
sentry.io › sentry answers › java › how do i declare and initialize an array in java?
How do I declare and initialize an array in Java? | Sentry
Here, you have to explicitly give the size of the array. In our example, we’ve created an array that can store 3 ints, all of which are zero. Here’s a runnable example of dynamically setting the values in an array.
🌐
Jenkov
jenkov.com › tutorials › java › arrays.html
Java Arrays
This example first sets the value of the element (int) with index 0, and second it reads the value of the element with index 0 into an int variable. You can use the elements in a Java array just like if they were ordinary variables.
🌐
Studytonight
studytonight.com › java-examples › how-to-convert-array-to-set-in-java
How to Convert Array to Set in Java - Studytonight
This example is useful if you want to use the stream to get set elements. import java.util.Arrays; import java.util.HashSet; import java.util.Set; import java.util.stream.Collectors; public class Main { public static void main(String[] args){ String[] fruits = {"Apple", "Orange", "Banana","Orange"}; for (int i = 0; i < fruits.length; i++) { System.out.println(fruits[i]); } Set<String> fruitsSet = new HashSet<>(); fruitsSet = Arrays.stream(fruits).collect(Collectors.toSet()); System.out.println(fruitsSet); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-in-java
Arrays in Java - GeeksforGeeks
An integer array arr is declared and initialized in the main method. The sum() method is called with arr as an argument. Inside the sum() method, all array elements are added using a for loop.
Published   December 21, 2016
🌐
Coderanch
coderanch.com › t › 614766 › java › Set-methods-arrays-righter
Get/Set methods and arrays? What's the right (or righter) way to do it? (Beginning Java forum at Coderanch)
The problem I get to is when I have a class that contains an array. How would I efficiently create a get and a set for an int[], boolean[], or WhatHaveYou[]? In a practice program, I created a SalesTeam class, I think I made a constructor and get/sets that works, but I'm really looking for one that is acceptable by convention/standard : Interesting. I am surprised that no one mentioned this yet... Or perhaps someone did, and I missed it. JavaBean properties actually defines a standard for array elements -- or specifically, Indexed Properties.