Skip to content
Java 8 Stream API Tutorial: 20 Practical Examples for Filtering, Mapping, Reducing, and More

Java 8 Stream API Tutorial: 20 Practical Examples for Filtering, Mapping, Reducing, and More

This comprehensive tutorial explains Java 8's Stream API and Lambda expressions, demonstrating how to create streams from collections and arrays, perform intermediate operations like filter, map, flatMap, and sorted, and use terminal operations such as forEach, findFirst, reduce, and collect through twenty detailed code examples covering employee data processing, aggregation, grouping, and sorting.

Architecture Digest
Architecture Digest
Architecture Digest

Java 8 introduced the Stream API and Lambda expressions, offering a concise and functional way to process collections and arrays.

A stream treats the source data as a flow, allowing intermediate operations (e.g., filter, map, flatMap, sorted, distinct, limit, skip) and terminal operations (e.g., forEach, findFirst, findAny, anyMatch, count, reduce, collect, joining).

Streams can be created from collections using collection.stream(), from arrays with Arrays.stream(array), or via static factory methods such as Stream.of(...), Stream.iterate(seed, f), and Stream.generate(supplier).

The tutorial provides twenty practical examples, including:

Filtering employees with salary greater than 8000 and collecting the results.

Sorting employees by salary descending and age ascending.

Finding maximum, minimum, sum, and count values in numeric collections.

Mapping strings to uppercase and adding a constant to integer elements.

Using flatMap to split and merge string arrays.

Reducing a list of integers to sum, product, and maximum.

Collecting stream results into List, Set, and Map structures.

Performing statistical aggregations such as counting, averaging, summing, and summarizing employee salaries.

Partitioning and grouping employees by salary thresholds, gender, and region.

Joining element strings with custom delimiters.

Combining streams, removing duplicates, limiting, and skipping elements.

Key code snippets:

List<Person> personList = new ArrayList<>();
personList.add(new Person("Tom", 8900, 23, "male", "New York"));
List<String> highEarners = personList.stream()
    .filter(p -> p.getSalary() > 8000)
    .map(Person::getName)
    .collect(Collectors.toList());
List<Integer> numbers = Arrays.asList(1,3,5,7,9,11);
int sum = numbers.stream().reduce(Integer::sum).orElse(0);
Map<String, List<Person>> byGender = personList.stream()
    .collect(Collectors.groupingBy(Person::getSex));

These examples illustrate how Stream operations simplify data processing, reduce boilerplate code, and improve readability compared to traditional iterative approaches.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javadata processingfunctional programmingcollectionsStream APIJava 8
Reader feedback

How this landed with the community

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.