Streams: mapToObj turns an IntStream back into a Stream of objects

mapToObj is the primitive-to-reference counterpart of boxed(), applying a function that turns each int into an object, such as a formatted label, in one step.

Code
List<String> labels = IntStream.rangeClosed(1, 5)
        .mapToObj(i -> "item-" + i)
        .toList();
System.out.println("Labels: " + labels);
Output
Labels: [item-1, item-2, item-3, item-4, item-5]
Advertisement
More in JAVA

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

© Java Coding Hub · About · Contact · Privacy · Terms