Quick revision
The short answer
Arrays.asList(nums) with an int[] gives a List<int[]> with one element, the array itself, because generics cannot hold primitives; and Arrays.asList(3, 1, 2) is a fixed-size list backed by an array, so set works but b.add(4) throws UnsupportedOperationException. list.toArray(new String[0]) returns a correctly typed String[], Stream.toList() returns an unmodifiable list so c.sort(null) throws, and going through a HashSet removes duplicates but loses the original order, which a LinkedHashSet would keep.
What to remember
In this answer, we went through the conversion lines one by one. Arrays.asList with an int[] gives a one-element List<int[]>, Arrays.asList with boxed values gives a fixed-size view where set works but add throws, toArray(new String[0]) is the right way to get a typed array, Stream.toList returns an unmodifiable list so sort throws, and a HashSet removes duplicates but loses order, which a LinkedHashSet or distinct() keeps.
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.
Arrays.asList(nums) with an int[] gives a List<int[]> with one element, the array itself, because generics cannot hold primitives; and Arrays.asList(3, 1, 2) is a fixed-size list backed by an array, so set works but b.add(4) throws UnsupportedOperationException. list.toArray(new String[0]) returns a correctly typed String[], Stream.toList() returns an unmodifiable list so c.sort(null) throws, and going through a HashSet removes duplicates but loses the original order, which a LinkedHashSet would keep.
Why this question is asked
Converting between arrays, lists, sets and streams is everyday work, and each conversion method has its own contract. Most of the time they do what we expect, but a few lines in this question compile without warnings and then fail or surprise at run time. Interviewers use code like this to check that we know the contract of each method, not just its name. In this answer, we will go through the lines in order, show the result of each, explain why the surprising ones behave as they do, and give the version that does what was meant.
1A Quick Recap: Primitives, Generics and Fixed-Size Views
Two pieces of background explain most of the surprises. First, Java generics work only with reference types. A List<int> is not allowed; the closest is List<Integer>, where each int is boxed into an Integer object. An int[] is itself an object, though, so it can be an element of a list.
Second, several conversion methods do not create ordinary ArrayLists. Arrays.asList creates a fixed-size view over an array, and Stream.toList creates an unmodifiable list. Both are typed simply as List, so the compiler cannot warn us when we call a method they do not support. For the code below, we assume list is a List<String> holding pear, apple and pear.
2Arrays.asList With an int[]
The first line looks like it should turn the array into a list of three numbers. It does not.
Arrays.asList takes a varargs parameter T... a. When we pass an Integer[], T is Integer, and the array becomes the varargs array, giving three elements. When we pass an int[], T cannot be int, because type parameters cannot be primitives, so the compiler chooses T = int[] and wraps our single array in a new one-element array. The declared type List<int[]> shows exactly what happened. The fix is to box the values with Arrays.stream(nums).boxed() or IntStream.of(nums).boxed().
3Arrays.asList(3, 1, 2) and add
With boxed values, Arrays.asList does give three elements. But the list it returns is a fixed-size view over the varargs array. Replacing an element is fine, since the array has a slot for it, but growing or shrinking the list is not possible, so add and remove throw.
Notice that sort works on b, because sorting only rearranges elements without changing the size. If we need a list that can grow, we copy it into an ArrayList. The class returned by Arrays.asList is a private class that happens to be named ArrayList inside Arrays, which is a frequent source of confusion when reading stack traces.
4ToArray(new String[0])
list.toArray() without arguments returns an Object[], and casting it to String[] fails with ClassCastException, because the array object really is an Object[]. Passing an array tells the method which array type to create.
When the given array is too small, as a zero-length array always is, toArray creates a new array of the same type and the right size. Older advice was to pass new String[list.size()] to avoid that extra allocation, but on modern HotSpot JVMs the zero-length form is as fast or faster, partly because the JVM can skip zeroing an array that it fills straight away. The presized form also has a race in concurrent collections whose size changes between size() and toArray. So new String[0], or String[]::new since Java 11, is the recommended form.
5Stream.toList() and sort
Stream.toList(), added in Java 16, returns an unmodifiable list. That is convenient for results that should not change, but code that sorts or adds to the result fails.
Note that collect(Collectors.toList()) happens to return an ArrayList today, but its documentation does not promise that, so code that needs a mutable list should say so with toCollection(ArrayList::new). Often the cleanest fix is to sort in the stream itself, as in the last line, and keep the unmodifiable result.
6Through a HashSet and Back
The last two lines remove duplicates by passing the list through a set, then turn the set back into a list.
| Expression | Result | Surprise |
|---|---|---|
Arrays.asList(intArray) | List<int[]> of size 1 | Primitives cannot be type arguments |
Arrays.asList(3, 1, 2) | Fixed-size view over an array | add and remove throw; set works |
list.toArray(new String[0]) | String[] of the right size | No-arg toArray gives Object[] |
stream.toList() | Unmodifiable list | sort and add throw |
new HashSet<>(list) | Set without duplicates | Original order lost |
new LinkedHashSet<>(list) | Set without duplicates | Keeps encounter order |
The duplicates disappear as intended, but the order changes. A HashSet orders its elements by their hash codes, not by when they were added, so the original pear-first order is lost, and with other data the order can even change as the set grows. A LinkedHashSet keeps encounter order, the order in which elements first appeared, and stream().distinct() does the same on an ordered stream.
7Follow-up questions
What is the difference between List.copyOf and Collections.unmodifiableList?
List.copyOf makes an independent unmodifiable copy, so later changes to the source do not show, and it rejects null elements. Collections.unmodifiableList wraps the original in a read-only view, so it costs no copy but shows every later change made through the original reference, and it allows nulls. List.copyOf can also skip the copy and return its argument when that is already an unmodifiable list of the same family, such as one from List.of.
Is there a direct way to turn an int[] into a List<Integer>?
The usual way is Arrays.stream(nums).boxed().toList(). Each int becomes an Integer object, which costs memory for large arrays, so for big numeric data it is often better to keep the int[] or use a primitive collection library.
Does Arrays.asList copy the array?
No. It wraps the array, so changes go both ways: b.set(0, 9) changes the array, and changing the array changes the list. That is useful but surprising when the array is reused later.
8Conclusion
In this answer, we went through the conversion lines one by one. Arrays.asList with an int[] gives a one-element List<int[]>, Arrays.asList with boxed values gives a fixed-size view where set works but add throws, toArray(new String[0]) is the right way to get a typed array, Stream.toList returns an unmodifiable list so sort throws, and a HashSet removes duplicates but loses order, which a LinkedHashSet or distinct() keeps.
Open with
Going line by line: Arrays.asList of nums, where nums is an int array, gives a List of int arrays with one element, the array itself, because generics cannot use primitives, so T is inferred as int array.
Then, if they want more
To get a list of numbers I would write Arrays.stream of nums, boxed, toList.
Arrays.asList of 3, 1, 2 gives a fixed-size list backed by an array, so set and sort work, but b.add of 4 throws UnsupportedOperationException; if I need to grow it, I copy it into a new ArrayList. list.toArray with new String of zero returns a correctly typed String array of the right size; the no-argument toArray returns Object array and casting it fails.
The zero-length form is as fast as presizing on modern JVMs, and since Java 11 I can also write toArray with String array constructor reference. Arrays.stream, boxed, toList gives an unmodifiable list, so c.sort of null throws UnsupportedOperationException.
I would either sort inside the stream, or collect with toCollection of ArrayList::new to get a mutable list. new HashSet of list removes duplicates, but HashSet has no order, so converting back to an ArrayList gives the elements in hash order, not the original order. If order matters, I use a LinkedHashSet or stream distinct, which keep encounter order.
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 Collections Framework, overview, java.util, package 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 "What does each line do, and which ones fail or surprise?": https://scrivio-interview-library.netlify.app/java/collections-overview/d69c49a113/