Ad space (banner)
โ˜•Java Lessons
Lesson 23 / 68

Transforming an Array (Stream map)

This lesson covers transforming an array with Java's Stream, so you can process every element at once. It's for anyone searching "Java Stream map usage."

Arrays.stream(array).map(function) applies the same operation to every element of an array, producing a new array (or stream). Think of it like a conveyor belt that processes every item passing through the same way.

The example creates a new array, doubled, with every element of {1, 2, 3} doubled, using Arrays.stream(nums).map(n -> n * 2).toArray(). The n -> n * 2 syntax is called a lambda expression, letting you define logic right on the spot.

A common early mistake is forgetting .toArray(). .map() alone leaves you with a lazily-evaluated Stream โ€” you need to explicitly convert it if you want an actual array.

In real projects, Stream methods can be chained together, letting you express several transformations or filters in a single line โ€” a frequently-used technique in real work.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Java directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)