Skip to content

Java Stream API and Functional Programming

Java Stream API and Functional Programming in Java refers to the paradigm shift introduced in Java 8 (and expanded in subsequent versions) that enables developers to write code in a more declarative and functional style^[200-編程思維__Java-Stream與函數式編程.md]。

The Stream API allows collections to be processed in a declarative way (specifying what to do rather than how to do it), while Functional Programming concepts (such as lambda expressions and method references) treat functions as first-class citizens^[200-編程思維__Java-Stream與函數式編程.md]。

Core Concepts

  • Streams vs Collections: Collections are in-memory data structures that store data. Streams are views of data that allow for lazy and potentially parallel operations. A Stream does not store data; it conveys elements from a source through a pipeline of computational operations^[200-編程思維__Java-Stream與函數式編程.md]。
  • Lazy Evaluation: Intermediate operations (like map, filter) are not executed until a terminal operation (like collect, forEach) is invoked. This allows for efficient processing by only performing work when necessary^[200-編程思維__Java-Stream與函數式編程.md]。
  • Functional Interfaces: The backbone of Java's functional implementation. Interfaces with a single abstract method (e.g., Function, Predicate, Consumer) can be implemented concisely using Lambda Expressions^[200-編程思維__Java-Stream與函數式編程.md]。

Stream Operations

Stream operations are typically divided into two categories:

Intermediate Operations

These operations transform a stream into another stream. They are lazy and can be chained together to form a processing pipeline^[200-編程思維__Java-Stream與函數式編程.md]。 * filter(Predicate): Excludes elements that do not match a condition^[200-編程思維__Java-Stream與函數式編程.md]。 * map(Function): Transforms elements from one type to another^[200-編程思維__Java-Stream與函數式編程.md]。 * sorted(Comparator): Sorts elements based on a comparator^[200-編程思維__Java-Stream與函數式編程.md]。

Terminal Operations

These operations produce a result or a side-effect. Once a terminal operation is invoked, the stream is considered consumed^[200-編程思維__Java-Stream與函數式編程.md]。 * collect(Collector): Accumulates elements into a collection (like List or Set) or other summary forms^[200-編程思維__Java-Stream與函數式編程.md]。 * forEach(Consumer): Performs an action for each element^[200-編程思維__Java-Stream與函數式編程.md]。 * reduce(BinaryOperator): Combines elements to produce a single summary result^[200-編程思維__Java-Stream與函數式編程.md]。 * count(): Returns the number of elements^[200-編程思維__Java-Stream與函數式編程.md]。 * anyMatch / allMatch: Returns boolean based on conditions^[200-編程思維__Java-Stream與函數式編程.md]。

Common Functional Interfaces

The java.util.function package provides several key interfaces^[200-編程思維__Java-Stream與函數式編程.md]:

Interface Method Usage
Function<T, R> R apply(T t) Transforms input to output.
Predicate boolean test(T t) Evaluates a condition (returns true/false).
Consumer void accept(T t) Performs an action on input, returns nothing.
Supplier T get() Supplies a value without input.

Parallelism

The Stream API simplifies parallel processing. By simply calling parallelStream() instead of stream(), the runtime environment automatically partitions the data and processes operations across multiple threads^[200-編程思維__Java-Stream與函數式編程.md]。

  • [[Java Lambda Expressions]]
  • [[Optional]]: A container object used to handle nulls elegantly, often used in conjunction with Streams.
  • [[Method References]]: A shorthand notation for lambdas that call specific methods.
  • 20/80 Learning Principle: Applying this principle can help prioritize the most common Stream operations (map, filter, collect) over niche ones.

Sources

  • 200-編程思維__Java-Stream與函數式編程.md