Java SE API

Stream.map()

Transforms each element with a stateless, noninterfering function. Java SE API; example uses Java 21-compatible syntax.

Formal Syntax & Structure

java syntaxjava
<R> Stream<R> map(Function<? super T, ? extends R> mapper)

Parameters & Attributes

NameTypeDescription
mapperFunction<? super T, ? extends R>Transformation

Return Value

Stream<R>

Transforms each element with a stateless, noninterfering function.

Working Example

Example Snippetjava
import java.util.*;import java.util.stream.*;public class Main {    public static void main(String[] args) {        record Item(String name, int units) {}        var items = List.of(new Item("pen", 4), new Item("book", 0), new Item("clip", 9));        System.out.println(items.stream().filter(i -> i.units() > 0).map(Item::name).toList());        System.out.println(items.stream().mapToInt(Item::units).sum());    }}
Java Reference