Java Streams Examples

Article
The code refers to the following articles:
and, in the package eu.happycoders.lambdas, to the articles
and, in the package eu.happycoders.functionalinterfaces, to the articles
and, in the package eu.happycoders.reduce, to the articles
and, in the package eu.happycoders.tomap, to the articles
and, in the package eu.happycoders.parallel and the directory benchmarks/parallel-streams, to the articles
and, in the package eu.happycoders.groupingby, to the articles
Contents
All examples work on the same data model: the records Book and Author, the enum Genre, and the Library class with eleven public-domain classics.
Every class with a main() method is one chapter of the article and prints the outputs shown there. Run one with java -cp target/classes eu.happycoders.streams.<ClassName> after mvn compile:
Ch1Intro – the same task as a loop and as a stream; the anatomy of a stream pipeline
Ch2Lambdas – lambda expressions, method references, functional interfaces
Ch3Sources – creating streams from collections, arrays, ranges, files, and generators
Ch4Intermediate – filter(), map(), flatMap(), mapMulti(), distinct(), sorted(), limit(), skip(), takeWhile(), dropWhile(), peek(), gather()
Ch5Terminal – forEach(), collect() and collectors, toList(), toArray(), reduce(), count(), min(), max(), findFirst(), anyMatch(), and the others
Ch6Lazy – lazy evaluation: element-by-element processing, short-circuiting, stateful operations, single use
Ch7Parallel – parallel streams
Ch8Mistakes – common mistakes and how to avoid them
The lambda article has its own chapters in eu.happycoders.lambdas, on the same data model (run them with java -cp target/classes eu.happycoders.lambdas.<ClassName>):
Ch1AnonymousVsLambda – the same comparator as an anonymous class and as a lambda
Ch2Syntax – parameters, expression and block body, explicit types, var, the unnamed parameter _
Ch3TargetType – the four contexts of a target type, functional interfaces, overloaded methods
Ch4VariableAccess – effectively final, the one-element-array workaround, this (with Scope and BookFilter)
Ch5MethodReferences – the four kinds of method references, this:: and super::, an ambiguous reference
Ch6WhereUsed – lambdas in Comparator, collections, maps, Optional, and threads
Ch7UnderTheHood – the generated class, identity of capturing and non-capturing lambdas (with LambdaDemo for javap)
Ch8Mistakes – a lambda that is too long, recursion, a lambda in a stack trace (with LambdaTrace)
The functional-interfaces article has its chapters in eu.happycoders.functionalinterfaces (run them with java -cp target/classes eu.happycoders.functionalinterfaces.<ClassName> from the repository root - two chapters read files relative to it):
Ch1Definition – Runnable as the simplest case; Comparator implemented by a named class (YearComparator) and by a lambda; the abstract method called directly; default methods
Ch2Annotation – @FunctionalInterface on TitleFormatter, the error for a second abstract method, an interface without the annotation
Ch3CoreFour – Function, Predicate, Consumer, Supplier with andThen(), compose(), identity(), and(), or(), negate(), not(), isEqual(), orElseGet(), toCollection()
Ch4Operators – UnaryOperator in List.replaceAll(), BinaryOperator in reduce(), maxBy(), Map.merge()
Ch5TwoParameters – BiFunction, BiConsumer, BiPredicate in Map.forEach(), computeIfPresent(), replaceAll(), Files.find()
Ch6Primitives – ToIntFunction, IntPredicate, IntUnaryOperator, IntFunction, IntBinaryOperator, IntSupplier, ObjIntConsumer on an IntStream; boxing
Ch7Wildcards – Predicate<? super T> and Function<? super T, ? extends R>, and a method of our own with and without the wildcard
Ch8OutsidePackage – Runnable, Callable, Comparator, FileFilter, DirectoryStream.Filter, TemporalAdjuster
Ch9Custom – TitleFormatter vs. Function (nominal typing), ThrowingFunction for checked exceptions
Ch10Mistakes – Function<T, Boolean> instead of Predicate, Function<T, Void> instead of Consumer, UnaryOperator<Integer> instead of IntUnaryOperator
The reduce() article has its chapters in eu.happycoders.reduce (run them with java -cp target/classes eu.happycoders.reduce.<ClassName>):
Ch1HowItWorks – the sum of 1 to 5 with reduce() and as a loop; every call of the accumulator
Ch2Variants – the three variants of reduce() on full and empty streams, and the two of IntStream
Ch3Applications – product, minimum with BinaryOperator.minBy(), a BigDecimal sum (with Prices) and a sum of the record Money, DoubleStream.sum() against reduce(), predicates and functions chained with reduce(), and and(), andThen() and allMatch() as the more readable alternatives
Ch4Parallel – accumulator and combiner calls in a parallel stream; a non-neutral identity, a non-associative accumulator, and the accumulator used as combiner, each with its fix
Ch5ReduceVsCollect – an ArrayList as identity, collect() and toList(), string concatenation against joining(), Collectors.reducing() as downstream collector
Ch6Fold – Gatherers.fold() in a parallel stream
Ch7Mistakes – get() on an empty Optional, mapToInt() instead of boxing, overflow with Math::multiplyExact, BigInteger
The toMap() article has its chapters in eu.happycoders.tomap (run them with java -cp target/classes eu.happycoders.tomap.<ClassName>):
Ch1HowItWorks – title to year with toMap() and as a loop; Function.identity() for a lookup map
Ch2Variants – the three variants of toMap(): a duplicate key, the merge function, TreeMap::new; a throwing merge function against collectingAndThen()
Ch3Merging – counting with Integer::sum, concatenating titles (and groupingBy() with joining()), the oldest book with BinaryOperator.minBy()
Ch4MapType – the order of the HashMap, LinkedHashMap in stream order, EnumMap per genre
Ch5Null – the NullPointerException for a null value with two and three arguments, filter() and collect() with three arguments as ways out, a null key in a TreeMap
Ch6Unmodifiable – toUnmodifiableMap(): UnsupportedOperationException, merge function, null key
Ch7Applications – filtering, transforming and inverting a map; two lists to one map
Ch8VsGroupingBy – lists and counts with toMap() and groupingBy(), nested maps with toMap() as downstream collector
Ch9Parallel – the later number wins: toMap() against toConcurrentMap() in a parallel stream
Ch10Mistakes – a merge function that returns null, and counting first as the fix; swallowed duplicates
The parallel streams article has its chapters in eu.happycoders.parallel (run them with java -cp target/classes eu.happycoders.parallel.<ClassName>); its JMH benchmarks are a Maven project of their own in benchmarks/parallel-streams:
Ch1Creating – parallelStream() and parallel(), the overflow of IntStream.sum(), the last parallel() or sequential() wins
Ch2HowItWorks – trySplit() on an ArrayList, the 128 pieces of one million elements, the threads that process a stream, the size of the common pool (with -XX:ActiveProcessorCount=1 or -Djava.util.concurrent.ForkJoinPool.common.parallelism=4)
Ch3WhenFaster – the growing blocks of a LinkedList, and the pieces of an ArrayList, a HashSet and a TreeSet split the way the stream does (with Pieces)
Ch4Order – forEach() against forEachOrdered(), findAny() against findFirst()
Ch5Rules – a shared ArrayList, a counting lambda and a stream of indices, an identity that is not neutral
Ch6Exceptions – ten exceptions among one million elements; whether the throwing thread keeps working
Ch7Blocking – a blocking parallel stream slows down a computing one
Ch8CustomPool – a parallel stream in a ForkJoinPool of its own
The groupingBy() article has its chapters in eu.happycoders.groupingby (run them with java -cp target/classes eu.happycoders.groupingby.<ClassName>):
Ch1HowItWorks – books by genre with groupingBy() and as a loop with computeIfAbsent(); the order of the HashMap keys
Ch2Variants – the three variants of groupingBy(): lists per author, counting() per genre, EnumMap, LinkedHashMap and TreeMap as map type
Ch3Downstream – mapping(), joining(), toList() against toCollection(TreeSet::new), counting() and summingInt(), averagingInt(), summarizingInt(), minBy(), collectingAndThen(), filtering() against filter(), flatMapping() over the authors, teeing() with minBy() and maxBy()
Ch4Nested – two levels: genre, then author
Ch5Keys – the decade as a computed key, a record as a composite key, a null key with filter() and Optional as ways out
Ch6Partitioning – partitioningBy() with and without a downstream collector; an empty partition against groupingBy()
Ch7Sorting – a map sorted by value into a LinkedHashMap, the top three, titles sorted within each group
Ch8Parallel – the order within the groups: groupingBy() against groupingByConcurrent() in a parallel stream
Ch9Mistakes – get() on a missing group, and getOrDefault() as the fix
The code requires Java 25 or newer.
Additional Resources
Java Versions PDF Cheat Sheet
Stay up-to-date with the latest Java features with this free PDF Cheat Sheet!

- Avoid lengthy research with this concise overview of all Java versions from Java 10 to Java 27.
- Discover the innovative features of each new Java version, summarized on a single page.
- Impress your team with your up-to-date knowledge of the latest Java version.
👉 Download the free Java Versions PDF
(Hier geht's zur deutschen Version → Java-Versionen PDF)
The Big O Cheat Sheet
With this free 1-page PDF cheat sheet, you'll always have the 7 most important complexity classes at a glance.

- Always choose the most efficient data structures and thus increase the performance of your applications.
- Be prepared for technical interviews and confidently present your algorithm knowledge.
- Become a sought-after problem solver and be known for systematically tackling complex problems.
👉 Download the free Big O Cheat Sheet
(Hier geht's zur deutschen Version → O-Notation Cheat Sheet)
HappyCoders Newsletter
👉 Want to stay on top of modern Java?
Sign up for the HappyCoders newsletter – Modern Java: new versions & features, performance, and JVM insights – once a month.
(Hier geht's zur deutschen Version → HappyCoders-Newsletter deutsch)
🇩🇪 An alle Java-Entwickler:innen, die durch fundierte Kenntnisse über Datenstrukturen besseren Code schreiben wollen
Trage dich jetzt unverbindlich auf die Warteliste von „Mastering Data Structures in Java“ ein, und erhalte das beste Angebot!

👉 Zur Warteliste