Comprehensive Guide to Java Stream API with Practical Examples
This article provides an in‑depth tutorial on Java 8 Stream API, covering creation, intermediate and terminal operations, filtering, mapping, reducing, collecting, grouping, sorting, and practical code examples for processing collections such as employee data.
This tutorial introduces the Java 8 Stream API, explaining how streams treat collections as pipelines of data that can be processed lazily.
It shows how to create streams from collections or arrays:
List<String> list = Arrays.asList("a", "b", "c");
Stream<String> stream = list.stream();
Stream<Integer> intStream = Stream.of(1, 2, 3);Common intermediate operations are demonstrated, including filter to select elements, map to transform them, and flatMap to flatten nested structures:
List<Person> highEarners = personList.stream()
.filter(p -> p.getSalary() > 8000)
.collect(Collectors.toList());
List<String> names = personList.stream()
.map(Person::getName)
.collect(Collectors.toList());Terminal operations such as forEach, findFirst, anyMatch, max, min, and count are illustrated with simple examples.
The article also covers aggregation with reduce and the richer collect API, showing how to gather results into lists, sets, maps, and how to perform statistical summaries:
Map<String, Person> highSalaryMap = personList.stream()
.filter(p -> p.getSalary() > 8000)
.collect(Collectors.toMap(Person::getName, p -> p));
Long total = personList.stream().collect(Collectors.counting());
Double avg = personList.stream().collect(Collectors.averagingDouble(Person::getSalary));Grouping and partitioning are explained using Collectors.groupingBy and Collectors.partitioningBy to categorize employees by gender, region, or salary thresholds.
Sorting is demonstrated with both natural ordering and custom comparators, including multi‑level sorting (salary then age):
List<String> sortedBySalary = personList.stream()
.sorted(Comparator.comparing(Person::getSalary).reversed())
.map(Person::getName)
.collect(Collectors.toList());Additional stream utilities such as distinct, limit, skip, and concat are presented for deduplication, pagination, and merging streams.
Throughout the guide, numerous complete code snippets illustrate each concept, making it a practical reference for developers working with Java collections and functional-style processing.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
