Backend Development 11 min read

Understanding Java 8 Stream API with Practical PO Examples

This article introduces Java 8 Stream API, explains its core operations such as filter, map, sorted, forEach, collect, statistics, and parallelStream, and demonstrates their usage with a UserPo class through comprehensive code examples, while also noting unrelated promotional offers.

Top Architect
Top Architect
Top Architect
Understanding Java 8 Stream API with Practical PO Examples

Java 8 introduced the Stream API, which allows declarative processing of collections similar to SQL queries.

The stream treats the source collection as a pipeline where operations such as filter, sorted, map, and collect can be applied.

For developers familiar with Linux pipelines, the concept is analogous to the pipe operator.

Using a PO class UserPo (with fields name and score ), the article demonstrates common stream operations:

public class UserPo {
    private String name;
    private Double score;
    // constructors, getters, setters omitted
}

filter : selects elements that satisfy a predicate, e.g., counting students with a non‑null score.

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, for example by score in descending order.

List
sortedList = 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 to every student's score.

sortedList.forEach(p -> p.setScore(p.getScore() + 10));

collect : aggregates results, demonstrated with grouping by score and converting to lists.

Map
> groupByScore = list.stream()
    .filter(p -> p.getScore() != null)
    .collect(Collectors.groupingBy(UserPo::getScore));

statistics : obtains summary statistics like max, min, sum, and average.

DoubleSummaryStatistics stats = list.stream()
    .filter(p -> p.getScore() != null)
    .mapToDouble(UserPo::getScore)
    .summaryStatistics();

parallelStream : runs stream operations in parallel to improve performance, with a note on its lack of thread‑local context.

long parallelCount = list.parallelStream()
    .filter(p -> p.getScore() != null)
    .count();

The article concludes with the full source code of UserPo and a StreamTest class that puts all the above operations together.

Additional sections of the page contain promotional offers for AI tools, private consulting, and paid membership groups, which are unrelated to the technical tutorial.

Javabackend developmentFunctional ProgrammingCode ExampleStream APIJava 8
Top Architect
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.