Backend Development 11 min read

Using Java 8 Stream API for Collection Processing with Practical Examples

This article introduces Java 8 Stream API, explaining its declarative approach to processing collections, and demonstrates common operations such as filter, map, sorted, forEach, collect, statistics, and parallelStream with a PO class example, while also including full source code and brief promotional notes.

Top Architect
Top Architect
Top Architect
Using Java 8 Stream API for Collection Processing with Practical Examples

Java 8 added the Stream API, a high‑level abstraction that lets developers process collections in a declarative, SQL‑like style.

The stream model treats a collection as a flow of elements that passes through a pipeline of operations such as filtering, mapping, sorting, and aggregation.

Below is a simple PO class used in the examples:

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

filter : removes elements that do not satisfy a predicate. Example – count students whose score is not null.

// Count students with a non‑null score
long count = list.stream()
    .filter(p -> p.getScore() != null)
    .count();

map : transforms each element into another form. Example – extract all scores into a list and concatenate all names.

// List of scores
List<Double> scoreList = list.stream()
    .map(UserPo::getScore)
    .collect(Collectors.toList());
// Comma‑separated names
String nameString = list.stream()
    .map(UserPo::getName)
    .collect(Collectors.joining(","));

sorted : orders the stream according to a comparator. Example – sort by score in descending order.

List<UserPo> sortedList = list.stream()
    .filter(p -> p.getScore() != null)
    .sorted(Comparator.comparing(UserPo::getScore).reversed())
    .collect(Collectors.toList());

forEach : performs an action on each element. Example – give every student an extra 10 points.

// Add 10 points to each student
list.stream().forEach(p -> p.setScore(p.getScore() + 10));

collect : gathers the stream into a collection or other result. Example – group students by score.

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

statistics : obtains summary statistics for numeric streams.

DoubleSummaryStatistics stats = list.stream()
    .filter(p -> p.getScore() != null)
    .mapToDouble(UserPo::getScore)
    .summaryStatistics();
System.out.println("Max: " + stats.getMax());
System.out.println("Min: " + stats.getMin());
System.out.println("Sum: " + stats.getSum());
System.out.println("Avg: " + stats.getAverage());

parallelStream : processes the stream in parallel, leveraging multiple threads for better performance when appropriate.

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

Full example combining the above operations:

public class StreamTest {
public static void main(String[] args) {
List
list = new ArrayList<>();
list.add(new UserPo("Alice", 10.0));
list.add(new UserPo("Bob", 50.0));
// ... more data ...
// Demonstrate filter, map, sorted, forEach, collect, statistics, parallelStream as shown earlier
}
}

In addition to the technical tutorial, the original article contains promotional messages for a ChatGPT‑related community, paid courses, and related offers.

backendjavalambdaCollectionsJava8Stream API
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.