Convert int Array to List<Integer> and Back in Java - Quiz

Total: 6 questions

1. 

Why does Arrays.asList(intArray) return a list with one element?

The method is declared as Arrays.asList(T... a), and the type parameter T can only be a reference type. An int[] is itself an object, so the compiler passes the whole array as one varargs argument and infers List<int[]> with a size of 1 instead of a three-element List<Integer>.

int[] numbers = {1, 2, 3};

// Wrong: a list of ONE element - the array itself
List<int[]> wrong = Arrays.asList(numbers);
System.out.println(wrong.size()); // 1

// Right: unbox through a stream first
List<Integer> right = Arrays.stream(numbers).boxed().toList();
System.out.println(right.size()); // 3

For an array of objects (Integer[], String[]) Arrays.asList() really does return a list of all elements, but that list has a fixed size: add() and remove() throw UnsupportedOperationException.

2. 

How do you convert an int[] array into a List<Integer>, and how does Stream.toList() differ from collect(Collectors.toList())?

The conversion takes three steps: Arrays.stream() creates an IntStream, boxed() wraps every int into an Integer, and a terminal operation collects the result into a list.

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

// Mutable list
List<Integer> list = Arrays.stream(numbers)
                           .boxed()
                           .collect(Collectors.toList());

// Java 16+: shorter, but the list is unmodifiable
List<Integer> immutable = Arrays.stream(numbers).boxed().toList();

Stream.toList() was added in Java 16 and always returns an unmodifiable list that permits null elements: add() or set() throw UnsupportedOperationException. Collectors.toList() returns a mutable list, in practice an ArrayList, but the exact implementation is not guaranteed by the specification.

3. 

How do you convert a List<Integer> back into an int[] array, and why is a null element dangerous here?

The reverse conversion mirrors the forward one: mapToInt() unboxes every Integer into an int, and toArray() builds the primitive array.

List<Integer> list = List.of(10, 20, 30, 40);

int[] array = list.stream()
                  .mapToInt(Integer::intValue)
                  .toArray();

System.out.println(Arrays.toString(array)); // [10, 20, 30, 40]

The same call works for an ArrayList<Integer>, because ArrayList is a List. The variant list.toArray(new int[0]) does not compile: toArray() only works with arrays of objects. If the list can contain null, unboxing fails with a NullPointerException — filter first with .filter(Objects::nonNull).

4. 

What is the difference between int[], Integer[] and List<Integer>, and how do you convert between them?

int[] is an array of primitives, Integer[] is an array of wrapper objects, and List<Integer> is a collection of those wrappers. They are three unrelated types: int[] a = integerArray; is a compile error, so every pair needs an explicit conversion.

// int[] -> Integer[]
int[] primitives = {1, 2, 3};
Integer[] boxed = Arrays.stream(primitives)
                        .boxed()
                        .toArray(Integer[]::new);

// Integer[] -> int[]
Integer[] source = {4, 5, 6};
int[] unboxed = Arrays.stream(source)
                      .mapToInt(Integer::intValue)
                      .toArray();

// Integer[] -> mutable List<Integer>
List<Integer> list = new ArrayList<>(Arrays.asList(source));

// List<Integer> -> Integer[]
Integer[] back = list.toArray(new Integer[0]);

Note that Arrays.asList(source) really does produce a three-element list for Integer[], because it is an array of objects. The identical code behaves differently for int[].

5. 

How do you convert an int[] array into an ArrayList<Integer> specifically, and not just a List?

List is an interface, and the implementation behind it is not guaranteed by the specification. When your method signature requires an actual ArrayList<Integer>, ask for it explicitly — with the Collectors.toCollection(ArrayList::new) collector or by wrapping an existing list.

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

// Option 1: collect straight into an ArrayList
ArrayList<Integer> arrayList = Arrays.stream(numbers)
        .boxed()
        .collect(Collectors.toCollection(ArrayList::new));

// Option 2: wrap an existing list
ArrayList<Integer> copy = new ArrayList<>(list);

arrayList.add(6); // works: the size is not fixed

Both options give you a fully mutable ArrayList that accepts add() and remove() — unlike the result of Stream.toList() or the fixed-size list returned by Arrays.asList().

6. 

How do you convert an array into a collection other than List — for example into a Set<Integer> or a LinkedList<Integer>?

Nothing in the recipe is specific to List — only the collector changes. The Arrays.stream(arr).boxed() part stays the same, while Collectors.toSet() or Collectors.toCollection(...) picks the Collection implementation you need.

int[] numbers = {3, 1, 2, 3};

// int[] -> Set<Integer> (duplicates are dropped)
Set<Integer> set = Arrays.stream(numbers).boxed().collect(Collectors.toSet());

// int[] -> LinkedList<Integer>
LinkedList<Integer> linked = Arrays.stream(numbers)
        .boxed()
        .collect(Collectors.toCollection(LinkedList::new));

// any Collection<Integer> -> int[]
int[] fromSet = set.stream().mapToInt(Integer::intValue).toArray();

The way back is identical for every collection: stream().mapToInt(Integer::intValue).toArray(). And for an array of objects no boxing step is needed at all: List<String> words = new ArrayList<>(Arrays.asList(stringArray));.

Page 1 of 1