Java Stream Collectors: groupingBy, toMap and Why Side Effects Break Streams | Effective Java L46
The Modern SDE
0:00 / 0:00
Java Stream Collectors: groupingBy, toMap and Why Side Effects Break Streams | Effective Java L46
2 просмотра · 13 ч назад
The Modern SDE
1,27 тыс. подписчиков
2 просмотра · 13 ч назад
Lecture 46 of the Effective Java Masterclass by Engineer X · Part 6: Lambdas and Streams.
In this lecture
• A real surprise on Java 21: peek with count() logs nothing, because count can skip the pipeline
• Streams are a paradigm: each stage a pure function, every lambda free of side effects
• The forEach-into-a-map anti-pattern vs groupingBy with a TreeMap factory and counting()
• Collectors as reduction strategies: toList, toSet, toCollection, toMap, groupingBy, partitioningBy, joining, minBy and maxBy
• Downstream collectors (counting, summing, mapping, collectingAndThen) and why collect(counting()) is never needed
• toMap's duplicate-key IllegalStateException, merge functions (last write wins, max, sum) and the map-factory form
• joining's delimiter trap when elements contain the delimiter
• Modern Java: Stream.toList (16) vs Collectors.toList vs toUnmodifiableList (10), teeing (12), filtering and flatMapping (9)
Chapters
0:00 Counted, never audited
1:08 A paradigm, not just an API
1:35 forEach that fills a map
2:35 Collectors, family by family
4:45 toMap and duplicate keys
5:39 Modern Java: three kinds of list
6:28 In the interview
7:06 Recap and quiz
Follows the topics of Effective Java, 3rd edition, by Joshua Bloch (Item 46: Prefer side-effect-free functions in streams). Every explanation, example and line of code in this lecture is original.
Previous lecture: L045 Java Streams vs Loops: When Streams Help and When a Plain Loop Wins
Next lecture: L047 Return a List or a Stream in Java? Choosing Return Types for Sequences
#Java #EffectiveJava #JavaInterview #SeniorJavaDeveloper #JavaBestPractices #EngineerX #JavaCollectors #JavaStreams #groupingBy