boxed() converts a primitive IntStream into a Stream<Integer>, which is required before collecting into a generic collection like List<Integer> or Set<Integer>.
List<Integer> boxed = IntStream.rangeClosed(1, 5)
.boxed()
.toList();
System.out.println("Boxed list: " + boxed);
System.out.println("Element type: " + boxed.get(0).getClass().getSimpleName());
Boxed list: [1, 2, 3, 4, 5]
Element type: Integer
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-09-27