Using Java 8 Stream API for Collection Processing – Concepts and Code Examples
This article introduces Java 8's Stream API, explains its declarative pipeline model for filtering, mapping, sorting, and collecting data, demonstrates each operation with a UserPo example, and provides complete runnable code including parallel streams and statistical summaries.
Java 8 introduced the Stream API, providing a declarative way to process collections similar to SQL queries.
The Stream pipeline treats a collection as a flow of elements that can be filtered, sorted, mapped, and collected.
Example PO class UserPo is used to demonstrate operations.
filter – selects elements meeting a predicate, e.g., counting non‑null scores.
long count = list.stream()
.filter(p -> p.getScore() != null)
.count();map – transforms each element, e.g., extracting scores or joining names.
List
scoreList = list.stream()
.map(UserPo::getScore)
.collect(Collectors.toList());
String nameString = list.stream()
.map(UserPo::getName)
.collect(Collectors.joining(","));sorted – orders elements, e.g., descending by score.
List
sorted = list.stream()
.filter(p -> p.getScore() != null)
.sorted(Comparator.comparing(UserPo::getScore).reversed())
.collect(Collectors.toList());forEach – performs an action on each element, such as adding 10 points.
sorted.forEach(p -> p.setScore(p.getScore() + 10));collect – aggregates results, demonstrated with grouping by score and converting to lists or strings.
Map
> byScore = list.stream()
.filter(p -> p.getScore() != null)
.collect(Collectors.groupingBy(UserPo::getScore));statistics – obtains summary statistics of numeric streams.
DoubleSummaryStatistics stats = sorted.stream()
.mapToDouble(UserPo::getScore)
.summaryStatistics();parallelStream – runs the pipeline in parallel to improve performance, with the same operations as above.
long parallelCount = list.parallelStream()
.filter(p -> p.getScore() != null)
.count();Full example code for UserPo class and a main method ( StreamTest ) is provided, showing creation of sample data and applying the described Stream operations.
public class UserPo {
private String name;
private Double score;
// constructors, getters, setters, toString...
}
public class StreamTest {
public static void main(String[] args) {
List
list = new ArrayList<>();
list.add(new UserPo("小一", 10d));
list.add(new UserPo("小五", 50d));
list.add(new UserPo("小六", 60d));
list.add(new UserPo("小6", 60d));
list.add(new UserPo("小空", null));
list.add(new UserPo("小九", 90d));
// apply stream operations as shown above
}
}Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.