Перейти к содержимому

Return a List or a Stream in Java? Choosing Return Types for Sequences | Effective Java L47

The Modern SDE

0:00 / 0:00

Return a List or a Stream in Java? Choosing Return Types for Sequences | Effective Java L47

1 просмотр · 4 ч назад
The Modern SDE
1,25 тыс. подписчиков
1 просмотр · 4 ч назад
Lecture 47 of the Effective Java Masterclass by Engineer X · Part 6: Lambdas and Streams. In this lecture • A real bug: an Iterable wrapped around one stream works once, then throws IllegalStateException • Return types before Java 8: collection interfaces, Iterable, arrays • Why you can't for-each a Stream, the cast workaround, and adapter methods in both directions (StreamSupport) • Collection is an Iterable with stream(), so it's usually the best public return type • Large but describable sequences: a custom AbstractList that builds elements on demand (72 seats, none stored) • Limits: int size(), cheap contains and size, or return a Stream or Iterable; ease of implementation can decide • Returning lazy, I/O-backed streams like Files.lines, and documenting who closes them • Modern Java: still no Iterable streams in 21; toList (16), toUnmodifiableList (10), List.copyOf, SequencedCollection (21) Chapters 0:00 The second loop 0:45 Before streams, it was easy 1:20 You can't for-each a stream 2:42 Collection: both worlds 3:36 Big, but easy to describe 5:15 When a Stream is right 5:52 Modern Java: still not Iterable 6:31 In the interview 7:13 Recap and quiz Follows the topics of Effective Java, 3rd edition, by Joshua Bloch (Item 47: Prefer Collection to Stream as a return type). Every explanation, example and line of code in this lecture is original. Previous lecture: L046 Java Stream Collectors: groupingBy, toMap and Why Side Effects Break Streams Next lecture: L048 Java Parallel Streams: Wrong Answers, Hangs, and When parallel() Pays Off #Java #EffectiveJava #JavaInterview #SeniorJavaDeveloper #JavaBestPractices #EngineerX #JavaStreams #JavaCollections #JavaAPIDesign