Streams: IntStream avoids boxing for numeric work

IntStream works on primitives, so no Integer objects are created. boxed() converts back when you need a collection.

Code
IntSummaryStatistics stats = IntStream.rangeClosed(1, 10).summaryStatistics();
System.out.println("sum 1..10 = " + stats.getSum() + ", avg = " + stats.getAverage());

List<Integer> squares = IntStream.rangeClosed(1, 5).map(n -> n * n).boxed().toList();
System.out.println("squares = " + squares);

System.out.println("chars = " + "java".chars().mapToObj(c -> (char) c).toList());
Output
sum 1..10 = 55, avg = 5.5
squares = [1, 4, 9, 16, 25]
chars = [j, a, v, a]
Advertisement

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-07-30