Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Data pipelines in Java

Data pipelines in Java: A Java data pipeline commonly combines a source, lazy Stream intermediate operations, and one terminal operation; elements are pulled only as needed.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

data pipelines

A Java data pipeline commonly combines a source, lazy Stream intermediate operations, and one terminal operation; elements are pulled only as needed.

Build it from Files.lines or collection.stream, apply filter/map/flatMap, then collect/reduce; choose IntStream, LongStream, or DoubleStream for primitives.

Mutating a non-concurrent source during pipeline execution violates non-interference, while a behavioral parameter that depends on mutable state violates statelessness. This does not prohibit changes before the terminal operation or changes supported by a concurrent source.

A Stream is single-use, I/O-backed pipelines must be closed, an infinite source needs limit or short-circuiting, and distinct/sorted may buffer the whole stream.

The pipeline keeps odd values, squares, deduplicates, and sorts them into [1, 9], then sums the result to 10.

Examples

import java.util.List;

public class Main {
    public static void main(String[] args) {
        var result = List.of(3, 1, 2, 3, 4).stream()
                .filter(n -> n % 2 == 1)
                .map(n -> n * n)
                .distinct()
                .sorted()
                .toList();
        System.out.println(result);
        System.out.println(result.stream().mapToInt(Integer::intValue).sum());
    }
}

Result

[1, 9]
10

The pipeline keeps odd values, squares, deduplicates, and sorts them into [1, 9], then sums the result to 10.

Sources