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
Syntax
stream.map(mapper)
Parameters
| Parameter | Description |
|---|---|
mapperFunction<T, R> |
The transformation. |
Return value
A new Stream<R>.
Examples
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.
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
| Version | Change |
|---|---|
| Java Java 8 | Added |
Related
stream.filter()· Keep only the elements that match a condition. Intermediate and lazy: nothing runs until a terminal operation.stream.mapToInt()· Map to an IntStream of primitive ints, which has sum(), average(), min(), max() and summaryStatistics().- Java Streams · tutorial