Streams: Arrays.stream() works on primitive arrays without boxing

Arrays.stream(int[]) returns an IntStream directly, letting sum and average run without boxing every element into an Integer first, unlike streaming a List<Integer>.

Code
int[] values = {5, 3, 8, 1};
int sum = Arrays.stream(values).sum();
double avg = Arrays.stream(values).average().orElse(0);
System.out.println("Sum: " + sum);
System.out.println("Average: " + avg);
String[] words = {"pear", "kiwi"};
long count = Arrays.stream(words).count();
System.out.println("String array count: " + count);
Output
Sum: 17
Average: 4.25
String array count: 2
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