Comprehensive Guide to Java Stream API with Practical Employee Data Examples
This article provides an in‑depth Java Stream API tutorial, covering stream creation, intermediate operations like filter, map, flatMap, and terminal operations such as forEach, reduce, collect, with numerous real‑world employee‑data examples and complete code samples.
The article introduces the Java Stream API, explaining that a Stream treats a collection of elements as a flow, enabling functional operations such as filtering, sorting, and aggregation without modifying the original data source.
It demonstrates how to create streams from collections and arrays using methods like list.stream(), list.parallelStream(), and Stream.of(), and shows the difference between sequential and parallel streams.
Intermediate operations are covered, including:
Filter: list.stream().filter(x -> x > 8000) Map and flatMap: transforming elements, e.g., list.stream().map(String::toUpperCase) or list.stream().flatMap(s -> Arrays.stream(s.split(","))) Distinct, sorted, limit, skip, and peek
Terminal operations such as forEach, anyMatch, findFirst, max, min, count, and reduce are illustrated with concrete examples that compute sums, products, and maximum values of integer collections.
The guide provides a full Person class example (name, salary, age, sex, area) and uses it to showcase common tasks:
List<Person> personList = new ArrayList<>();
personList.add(new Person("Tom", 8900, 23, "male", "New York"));
// ... other persons ...
List<String> highEarners = personList.stream()
.filter(p -> p.getSalary() > 8000)
.map(Person::getName)
.collect(Collectors.toList());
System.out.println("高于8000的员工姓名:" + highEarners);Aggregation operations using Collectors are explained, including toList, toSet, toMap, counting, averaging, summarizing, grouping, partitioning, and joining. Example of grouping by gender and area:
Map<String, Map<String, List<Person>>> group = personList.stream()
.collect(Collectors.groupingBy(Person::getSex,
Collectors.groupingBy(Person::getArea)));
System.out.println(group);Sorting is shown with natural order and custom comparators, e.g., sorting employees by salary descending and age descending when salaries are equal:
List<String> sorted = personList.stream()
.sorted((p1, p2) -> {
int cmp = Integer.compare(p2.getSalary(), p1.getSalary());
return cmp != 0 ? cmp : Integer.compare(p2.getAge(), p1.getAge());
})
.map(Person::getName)
.collect(Collectors.toList());
System.out.println(sorted);Finally, the article covers stream combination, deduplication, limiting, and skipping elements, demonstrating Stream.concat, distinct, limit, and skip operations.
Overall, the tutorial offers a hands‑on, example‑driven walkthrough of the Java Stream API, suitable for backend developers seeking to write concise, functional‑style code for collection 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.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
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.
