Using Java 8 Stream API for Collection Processing
This article introduces Java 8's Stream API, explains its functional style for handling collections, demonstrates common operations such as filter, map, sorted, forEach, collect, statistics, and parallelStream with concrete POJO examples, and provides a complete runnable code sample.
Java 8 added the Stream API, a high‑level abstraction that lets developers process collections in a declarative, SQL‑like manner. A stream represents a sequence of elements flowing through a pipeline where intermediate operations (filter, map, sorted, etc.) transform the data and a terminal operation produces a result.
The tutorial uses a simple UserPo POJO (with name and score fields) to illustrate each operation:
filter : selects elements that satisfy a predicate, e.g., list.stream().filter(p -> p.getScore() != null).count();
map : transforms each element, e.g., extracting scores with list.stream().map(UserPo::getScore).collect(Collectors.toList()); or joining names with Collectors.joining(",") .
sorted : orders elements, for example descending by score using .sorted(Comparator.comparing(UserPo::getScore).reversed()) .
forEach : performs an action on each element, such as adding 10 points to every student's score.
collect : aggregates results, demonstrated with groupingBy(UserPo::getScore) to produce a map of scores to student lists, as well as collecting to a list or a comma‑separated string.
statistics : obtains summary statistics (max, min, sum, average) via DoubleSummaryStatistics stats = stream.mapToDouble(UserPo::getScore).summaryStatistics(); .
parallelStream : runs the pipeline in parallel to leverage multiple CPU cores, shown with a parallel count operation.
The article also includes the full source code for the UserPo class and a StreamTest class that creates a list of UserPo objects and applies the above operations, printing intermediate and final results.
public class UserPo {
private String name;
private Double score;
// constructors, getters, setters omitted for brevity
@Override
public String toString() {
return "UserPo{name='" + name + "', score=" + score + "}";
}
} public class StreamTest {
public static void main(String[] args) {
List
list = new ArrayList<>();
list.add(new UserPo("小一", 10.0));
list.add(new UserPo("小五", 50.0));
// ... more data
long count = list.stream().filter(p -> p.getScore() != null).count();
System.out.println("参加考试的学生人数:" + count);
// other operations: map, sorted, forEach, collect, statistics, parallelStream
}
}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.