Java SE API

Stream.mapToInt()

Produces a primitive int stream for numeric processing. Java SE API; example uses Java 21-compatible syntax.

Formal Syntax & Structure

java syntaxjava
IntStream mapToInt(ToIntFunction<? super T> mapper)

Parameters & Attributes

NameTypeDescription
mapperToIntFunction<? super T>Maps input to int

Return Value

IntStream

Produces a primitive int stream for numeric processing.

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