Master Java 8 Streams: 20 Real‑World Examples to Boost Your Coding Efficiency
This tutorial introduces Java 8 Stream API, explains its core concepts, shows how to create streams from collections and arrays, and demonstrates 20 practical examples covering filtering, mapping, reducing, collecting, grouping, sorting and parallel processing to dramatically improve development productivity.
1. Stream Overview
Java 8 introduced the Stream API together with Lambda expressions, providing a powerful way to process collections as a flow of elements.
Stream treats a collection as a flow, allowing operations such as filter, sort, and aggregate via the Stream API.
2. Creating Streams
Streams can be created from collections, arrays, or static factory methods.
List<String> list = Arrays.asList("a", "b", "c");
Stream<String> stream = list.stream();
Stream<String> parallel = list.parallelStream();
int[] arr = {1, 3, 5, 6, 8};
IntStream intStream = Arrays.stream(arr);
Stream<Integer> s1 = Stream.of(1, 2, 3, 4, 5, 6);
Stream<Integer> s2 = Stream.iterate(0, x -> x + 3).limit(4);
Stream<Double> s3 = Stream.generate(Math::random).limit(3);3. Common Operations
3.1 Traversal / Matching
list.stream().filter(x -> x > 6).forEach(System.out::println);
Optional<Integer> first = list.stream().filter(x -> x > 6).findFirst();
Optional<Integer> any = list.parallelStream().filter(x -> x > 6).findAny();
boolean anyMatch = list.stream().anyMatch(x -> x > 6);3.2 Filtering
list.stream().filter(x -> x > 7).forEach(System.out::println);
List<String> highEarners = personList.stream()
.filter(p -> p.getSalary() > 8000)
.map(Person::getName)
.collect(Collectors.toList());3.3 Aggregation (max / min / count)
Optional<String> longest = strings.stream()
.max(Comparator.comparing(String::length));
Optional<Integer> maxInt = ints.stream().max(Integer::compareTo);
long count = ints.stream().filter(x -> x > 6).count();3.4 Mapping (map / flatMap)
String[] upper = strArr.stream()
.map(String::toUpperCase)
.toArray(String[]::new);
List<Integer> plusThree = ints.stream()
.map(x -> x + 3)
.collect(Collectors.toList());3.5 Reduction (reduce)
int sum = ints.stream().reduce(0, Integer::sum);
int product = ints.stream().reduce((a, b) -> a * b).orElse(0);
int maxVal = ints.stream().reduce(Integer::max).orElse(0);3.6 Collecting (collect)
Collect results into List, Set, Map, etc.
List<Integer> evens = list.stream()
.filter(x -> x % 2 == 0)
.collect(Collectors.toList());
Map<String, Person> highEarnersMap = personList.stream()
.filter(p -> p.getSalary() > 8000)
.collect(Collectors.toMap(Person::getName, p -> p));3.7 Grouping and Partitioning
Map<Boolean, List<Person>> bySalary = personList.stream()
.collect(Collectors.partitioningBy(p -> p.getSalary() > 8000));
Map<String, List<Person>> bySex = personList.stream()
.collect(Collectors.groupingBy(Person::getSex));
Map<String, Map<String, List<Person>>> bySexAndArea = personList.stream()
.collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(Person::getArea)));3.8 Sorting
List<String> sorted = personList.stream()
.sorted(Comparator.comparing(Person::getSalary).reversed()
.thenComparing(Person::getAge))
.map(Person::getName)
.collect(Collectors.toList());3.9 Stream Combination
List<String> merged = Stream.concat(stream1, stream2)
.distinct()
.collect(Collectors.toList());
List<Integer> limited = Stream.iterate(1, x -> x + 2)
.limit(10)
.collect(Collectors.toList());
List<Integer> skipped = Stream.iterate(1, x -> x + 2)
.skip(1)
.limit(5)
.collect(Collectors.toList());4. Visual Illustrations
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.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
