Quick revision
The short answer
int[] to List<Integer> is Arrays.stream(a).boxed().toList(), and the trap is Arrays.asList(a), which gives a List<int[]> with one element. List<Integer> to int[] is list.stream().mapToInt(Integer::intValue).toArray(), which throws NullPointerException on a null element. String[] to a resizable list is new ArrayList<>(Arrays.asList(arr)), because Arrays.asList alone is fixed-size and backed by the array, and List.of copies but is immutable and rejects nulls. List<String> to String[] is list.toArray(String[]::new) since Java 11, or list.toArray(new String[0]). Remember too that Stream.toList() returns an unmodifiable list.
What to remember
In this answer, we went through four conversions and their traps. int[] to List<Integer> needs Arrays.stream(a).boxed().toList(), because Arrays.asList(a) makes a List<int[]> of size 1. List<Integer> to int[] uses mapToInt(Integer::intValue).toArray(), which throws on null elements unless we handle them. A resizable list from String[] is new ArrayList<>(Arrays.asList(arr)), since Arrays.asList is a fixed-size view backed by the array and List.of is immutable and rejects nulls. List<String> to String[] is toArray(String[]::new) or toArray(new String[0]), and Stream.toList() always returns an unmodifiable list.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
int[] to List<Integer> is Arrays.stream(a).boxed().toList(), and the trap is Arrays.asList(a), which gives a List<int[]> with one element. List<Integer> to int[] is list.stream().mapToInt(Integer::intValue).toArray(), which throws NullPointerException on a null element. String[] to a resizable list is new ArrayList<>(Arrays.asList(arr)), because Arrays.asList alone is fixed-size and backed by the array, and List.of copies but is immutable and rejects nulls. List<String> to String[] is list.toArray(String[]::new) since Java 11, or list.toArray(new String[0]). Remember too that Stream.toList() returns an unmodifiable list.
Background to this question
Arrays and collections meet at every boundary: an old API returns String[], a stream produces a List, a library wants int[]. Each conversion has a one-line answer, but several of the obvious one-liners are subtly wrong, and the bugs they cause, a list of size 1, an UnsupportedOperationException far from the conversion, a NullPointerException in a mapping, are common in real code reviews. In this answer, we will recap why primitive arrays and generic lists do not mix directly, go through each of the four conversions with its trap, look at which lists can be changed afterwards, and answer the follow-up about toArray(new String[0]).
1A Quick Recap: Primitives, Boxing and Generic Lists
Generics in Java work only with reference types, so there is no List<int>. A List<Integer> holds Integer objects, and converting from int[] means boxing each int into an Integer; converting back means unboxing. A String[] is already an array of references, so it can be viewed as a List<String> without any conversion of its elements.
That difference explains most of the traps below: methods that work well for String[] behave unexpectedly for int[], because an int[] is itself one object, not a group of objects.
2Int[] to List<Integer>
The tempting call is Arrays.asList(a). It compiles, but not the way we hope.
Arrays.asList takes T... elements. With a String[], T is String and the array is the varargs array. With an int[], T cannot be int, so T becomes int[] and the int[] is a single element. The result is a list of size 1 holding our array, a classic bug that usually surfaces as contains(2) returning false.
Arrays.stream(a) gives an IntStream, boxed() turns it into a Stream<Integer>, and toList() collects it. Stream.toList(), added in Java 16, returns an unmodifiable list, so if we need to add elements later, we collect into an ArrayList instead.
3List<Integer> to int[]
Going back means unboxing every element, which the stream API does with mapToInt.
An int cannot be null, so unboxing a null Integer throws NullPointerException. If the list can contain nulls, we decide explicitly whether to skip them, as above, or to map them to a default with mapToInt(i -> i == null ? 0 : i). The decision depends on what null means in the data, so it should be visible in the code.
4String[] to a List That Can Be Resized
Here there are three candidates, and they produce three very different lists.
| Expression | Copies? | set / add | nulls |
|---|---|---|---|
Arrays.asList(arr) | No, backed by arr | set yes, add no | allowed |
List.of(arr) | Yes | neither | NullPointerException |
new ArrayList<>(Arrays.asList(arr)) | Yes | both | allowed |
Arrays.stream(arr).toList() | Yes | neither | allowed |
Arrays.asList does not copy. It wraps the array, so writes through the list change the array and the other way round, and since an array cannot grow, add and remove throw UnsupportedOperationException. List.of copies the elements into an immutable list and rejects null elements, which is often what we want for constants but not here. new ArrayList<>(Arrays.asList(arr)) copies the elements into a real, resizable ArrayList that is independent of the array, which is the answer to the question.
5List<String> to String[]
Collection.toArray() without arguments returns an Object[], which cannot be cast to String[]. To get a String[], we tell toArray which array type to create.
Both toArray(String[]::new) and toArray(new String[0]) return a correctly sized String[]. The generator form reads a little better and avoids the throwaway empty array.
6Follow-up questions
Why is toArray(new String[0]) preferred over a presized array?
With new String[list.size()], we allocate an array, which the JVM must zero, and then the collection fills it. With new String[0], the collection allocates the right size itself, and HotSpot can skip the zeroing because it knows every slot will be written straight away, so in benchmarks the empty-array version is as fast or faster. It is also safer for concurrent collections: if the size changes between size() and toArray, a presized array can end up with a trailing null or be replaced anyway. So new String[0], or String[]::new, is both simpler and at least as fast.
Is Arrays.asList ever the right choice?
Yes, when we want a cheap fixed-size List view of an array, for example to pass a String[] to a method that takes a List and only reads it, or to use List methods such as indexOf on the array. We just must not add or remove, and we should remember that writes go through to the array.
What is the difference between Stream.toList() and Collectors.toList()?
Stream.toList(), from Java 16, always returns an unmodifiable list and allows null elements. Collectors.toList() makes no promise about mutability; today it returns an ArrayList, but the documentation does not guarantee that. When we need a mutable list, Collectors.toCollection(ArrayList::new) states it explicitly.
7Conclusion
In this answer, we went through four conversions and their traps. int[] to List<Integer> needs Arrays.stream(a).boxed().toList(), because Arrays.asList(a) makes a List<int[]> of size 1. List<Integer> to int[] uses mapToInt(Integer::intValue).toArray(), which throws on null elements unless we handle them. A resizable list from String[] is new ArrayList<>(Arrays.asList(arr)), since Arrays.asList is a fixed-size view backed by the array and List.of is immutable and rejects nulls. List<String> to String[] is toArray(String[]::new) or toArray(new String[0]), and Stream.toList() always returns an unmodifiable list.
Open with
For int[] to List<Integer>, I use Arrays.stream(a).boxed().toList(). The trap is Arrays.asList(a): because generics can't use int, T becomes int[], so I get a List<int[]> with one element. Also, toList() gives an unmodifiable list, so if I need to add to it, I collect into an ArrayList.
Then, if they want more
Going back, List<Integer> to int[], I use list.stream().mapToInt(Integer::intValue).toArray(), and I have to think about nulls, because unboxing a null throws NullPointerException, so I either filter them or map them to a default. For String[] to a list I can resize, it's new ArrayList<>(Arrays.asList(arr)).
Arrays.asList on its own is a fixed-size view backed by the array, so set writes through to the array and add throws UnsupportedOperationException. List.of copies, but it's immutable and throws on null elements. And for List<String> to String[], I use list.toArray(String[]::new) on Java 11 or later, or list.toArray(new String[0]).
The plain toArray() returns an Object[], which can't be cast to String[]. If asked why new String[0] instead of a presized array: the collection allocates the right size itself, the JVM can skip zeroing it, so it's as fast or faster, and it's safer if the collection is changing concurrently.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 11, Java 16
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Convert between these forms and point out the traps: int[] to List<Integer>...": https://scrivio-interview-library.netlify.app/java/arrays/e0778615f2/