Java Optional and Empty Collections: Stop Returning null | Effective Java L53
The Modern SDE
0:00 / 0:00
Java Optional and Empty Collections: Stop Returning null | Effective Java L53
0 просмотров · 6 ч назад
The Modern SDE
1,26 тыс. подписчиков
0 просмотров · 6 ч назад
Lecture 53 of the Effective Java Masterclass by Engineer X · Part 7: Methods.
In this lecture
• A food app's order-again card that crashes for a brand-new customer because a method returned null
• Return empty collections and zero-length arrays, never null, and why the performance argument fails
• Shared immutable empties (Collections.emptyList, a static empty array) and toArray without presizing
• Optional for a single value that may be missing: of, ofNullable, empty, and never a null Optional
• orElse vs orElseGet (eager vs lazy), orElseThrow, map/filter/flatMap, or, ifPresentOrElse, Optional::stream
• Where Optional doesn't belong: containers, map values, fields; OptionalInt for primitives; its cost
• Modern Java: List.of() is null-hostile, toArray(String[]::new), requireNonNullElse, Optional.isEmpty and orElseThrow()
Chapters
0:00 A new customer crashes the app
0:35 Empty, not null
1:17 But what about allocation?
2:13 Optional for one maybe-value
3:12 Using an Optional
4:58 Where Optional doesn't belong
5:37 Modern Java: sharp edges
6:34 In the interview
7:21 Recap and quiz
Follows the topics of Effective Java, 3rd edition, by Joshua Bloch (Item 54: Return empty collections or arrays, not nulls, Item 55: Return optionals judiciously). Every explanation, example and line of code in this lecture is original.
Previous lecture: L052 Java Overloading vs Overriding: Why list.remove(1) Removes the Wrong Element
Next lecture: L054 How to Write Javadoc in Java: @param, @return, @throws and the Summary Line
#Java #EffectiveJava #JavaInterview #SeniorJavaDeveloper #JavaBestPractices #EngineerX #JavaOptional #NullSafety #JavaCollections #EffectiveJava