Java 25 (LTS) reference · Stream API

Java Stream map() Method

Transform each element with a function and return a stream of the results.

Java 25 (LTS) Since Java 8

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

stream.map(mapper)

Parameters

ParameterDescription
mapper
Function<T, R>
The transformation.

Return value

A new Stream<R>.

Examples

Transform elements
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<String> names = List.of("ana", "ben");
        System.out.println(names.stream().map(String::toUpperCase).toList());
        System.out.println(names.stream().map(String::length).toList());
    }
}

Output

[ANA, BEN]
[3, 3]

Run this on your computer — see “Get Started” for setup.

Format with tax
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Double> prices = List.of(10.0, 25.5);
        System.out.println(prices.stream().map(p -> "$%.2f".formatted(p * 1.2)).toList());
    }
}

Output

[$12.00, $30.60]

Run this on your computer — see “Get Started” for setup.

Version history

VersionChange
Java Java 8Added

Was this page helpful?