Facebook Pixel
Searching...
English
EnglishEnglish
EspañolSpanish
简体中文Chinese
FrançaisFrench
DeutschGerman
日本語Japanese
PortuguêsPortuguese
ItalianoItalian
한국어Korean
РусскийRussian
NederlandsDutch
العربيةArabic
PolskiPolish
हिन्दीHindi
Tiếng ViệtVietnamese
SvenskaSwedish
ΕλληνικάGreek
TürkçeTurkish
ไทยThai
ČeštinaCzech
RomânăRomanian
MagyarHungarian
УкраїнськаUkrainian
Bahasa IndonesiaIndonesian
DanskDanish
SuomiFinnish
БългарскиBulgarian
עבריתHebrew
NorskNorwegian
HrvatskiCroatian
CatalàCatalan
SlovenčinaSlovak
LietuviųLithuanian
SlovenščinaSlovenian
СрпскиSerbian
EestiEstonian
LatviešuLatvian
فارسیPersian
മലയാളംMalayalam
தமிழ்Tamil
اردوUrdu
Java 8 in Action

Java 8 in Action

Lambdas, Streams, and functional-style programming
by Raoul-Gabriel Urma 2014 424 pages
4.35
500+ ratings
Listen

Key Takeaways

1. Java 8 introduces lambda expressions and functional interfaces for concise, flexible code

Lambda expressions can make your code more readable and flexible.

Concise syntax. Lambda expressions provide a way to pass behavior as a method argument, enabling more flexible and reusable code. They are essentially anonymous functions that can be used wherever a functional interface is expected. Functional interfaces are interfaces with a single abstract method, such as Predicate, Function, and Consumer.

Improved readability. By using lambda expressions, you can write more expressive and readable code. For example, instead of verbose anonymous inner classes, you can use compact lambda syntax:

  • Before: new Comparator<Apple>() { public int compare(Apple a1, Apple a2) { return a1.getWeight().compareTo(a2.getWeight()); } }
  • After: (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight())

Method references. Java 8 also introduces method references as a shorthand notation for lambdas that simply call an existing method. This further enhances code readability and reusability.

2. The Streams API enables declarative, parallelizable data processing

Streams can be processed in parallel transparently, without you having to write any multithreaded code!

Declarative programming. The Streams API allows you to express complex data processing queries in a declarative way. Instead of writing how to perform operations step-by-step, you describe what you want to achieve. This leads to more concise and readable code.

Parallelism. Streams can be easily parallelized to take advantage of multicore processors. By simply changing stream() to parallelStream(), you can potentially improve performance without changing the logic of your code.

Lazy evaluation. Stream operations are lazily evaluated, meaning that computation on the source data is only performed when the terminal operation is initiated. This can lead to significant performance improvements, especially when dealing with large datasets.

Key Stream operations include:

  • Intermediate: filter, map, flatMap, distinct, sorted, limit
  • Terminal: forEach, count, collect, reduce, find, match

3. Default methods in interfaces allow API evolution without breaking existing implementations

Default methods help library designers evolve APIs in a backward-compatible way.

API evolution. Default methods allow you to add new methods to interfaces without breaking existing implementations. This feature was primarily introduced to evolve the Java collections API, enabling the addition of new methods like stream() to existing interfaces.

Multiple inheritance of behavior. With default methods, Java now supports a limited form of multiple inheritance of behavior. Interfaces can provide implementation code, not just method signatures.

Resolution rules. When a class implements multiple interfaces with conflicting default methods, Java 8 provides clear resolution rules:

  1. Classes always win over interfaces
  2. More specific interfaces win over less specific ones
  3. If there's still a conflict, the class must explicitly override the method

4. Optional class improves null handling and API design

Using Optional can help you design better APIs in which, just by reading the signature of a method, users can tell whether to expect an optional value.

Null alternative. The Optional class provides a better way to represent the absence of a value, rather than using null references. This helps prevent NullPointerExceptions and makes code more readable.

API design. By using Optional in method signatures, you explicitly communicate that a value might be absent. This leads to better API design and forces users to consider the case where a value is not present.

Optional provides several methods for safe handling:

  • isPresent() to check if a value exists
  • ifPresent(Consumer<T>) to execute an action if a value is present
  • orElse(T) to provide a default value
  • orElseGet(Supplier<T>) to lazily provide a default value
  • map(Function<T,U>) and flatMap(Function<T,Optional<U>>) for transformations

5. New Date and Time API addresses shortcomings of previous date/time classes

The date-time objects of the new Date and Time API are all immutable.

Immutability. The new java.time package introduces immutable date-time classes, which are thread-safe and prevent many common programming errors associated with mutable date-time classes.

Separation of concerns. The API clearly separates:

  • Human-readable time (LocalDate, LocalTime, LocalDateTime)
  • Machine time (Instant)
  • Time zones (ZonedDateTime)
  • Time periods (Duration, Period)

Fluent API. The new classes provide a fluent API for performing operations, making date and time manipulation more intuitive and less error-prone. For example:

LocalDate date = LocalDate.of(2023, Month.MAY, 15)
                           .plusDays(7)
                           .minusMonths(1);

6. CompletableFuture facilitates asynchronous and reactive programming

CompletableFuture allows you to express complex asynchronous computations in a declarative way.

Asynchronous programming. CompletableFuture extends the Future interface, providing a way to perform asynchronous computations and combine their results. This is particularly useful for building responsive applications and efficiently utilizing system resources.

Composability. CompletableFuture allows you to chain multiple asynchronous operations, making it easy to express complex workflows. Methods like thenApply, thenCompose, and thenCombine enable you to transform and combine results of asynchronous computations.

Key features:

  • Non-blocking operations
  • Exception handling with exceptionally and handle methods
  • Ability to combine multiple futures with allOf and anyOf
  • Support for timeout handling

7. Functional programming concepts enhance Java's expressiveness and maintainability

Functional-style programming promotes side-effect-free methods and declarative programming.

Immutability. Functional programming emphasizes the use of immutable data structures, which leads to more predictable and maintainable code. In Java 8, this is supported through the use of final variables and immutable collections.

Higher-order functions. Java 8 introduces the ability to pass functions as arguments and return them as results, enabling powerful abstractions and code reuse. This is achieved through lambda expressions and method references.

Declarative style. Functional programming encourages a declarative style of programming, where you describe what you want to achieve rather than how to achieve it. This often leads to more concise and readable code.

Key functional programming concepts in Java 8:

  • Pure functions (methods without side effects)
  • Function composition
  • Lazy evaluation (as seen in the Streams API)
  • Recursion as an alternative to iteration

8. Scala comparison highlights Java 8's functional programming capabilities and limitations

Scala provides a larger set of features around these ideas compared to Java 8.

Syntactic differences. Scala offers more concise syntax for many operations, such as creating and working with collections. For example, creating a map in Scala:

val authorsToAge = Map("Raoul" -> 23, "Mario" -> 40, "Alan" -> 53)

Advanced functional features. Scala provides more advanced functional programming features, including:

  • Pattern matching
  • Lazy evaluation
  • Currying
  • Traits (similar to Java interfaces but with more capabilities)

Type system. Scala has a more advanced type system, including features like:

  • Type inference
  • Higher-kinded types
  • Implicit conversions

While Java 8 introduces many functional programming concepts, it still maintains backward compatibility and a more gradual learning curve compared to Scala's more comprehensive functional programming approach.

Last updated:

Review Summary

4.35 out of 5
Average of 500+ ratings from Goodreads and Amazon.

Java 8 in Action receives high praise for its comprehensive coverage of Java 8 features, particularly lambdas and streams. Readers appreciate its clear explanations, practical examples, and insights into functional programming. Many find it helpful for updating their Java knowledge and bridging the gap between older versions and modern Java. The book's focus on Java 8 is both a strength and weakness, as some content becomes outdated with newer Java releases. Despite this, it remains highly recommended for Java developers looking to enhance their understanding of functional programming concepts and Java 8 features.

Your rating:

About the Author

Raoul-Gabriel Urma is a respected author and expert in the field of Java programming. While specific biographical information is not provided in the given content, his work "Java 8 in Action" has been widely acclaimed by readers and professionals in the software development community. Urma's writing style is praised for its clarity, depth, and ability to explain complex concepts in an accessible manner. His expertise in Java 8 features, particularly functional programming concepts, is evident throughout the book. Urma's approach combines theoretical knowledge with practical examples, making his work valuable for both newcomers and experienced Java developers seeking to update their skills.

Download PDF

To save this Java 8 in Action summary for later, download the free PDF. You can print it out, or read offline at your convenience.
Download PDF
File size: 0.60 MB     Pages: 9

Download EPUB

To read this Java 8 in Action summary on your e-reader device or app, download the free EPUB. The .epub digital book format is ideal for reading ebooks on phones, tablets, and e-readers.
Download EPUB
File size: 3.27 MB     Pages: 8
0:00
-0:00
1x
Dan
Andrew
Michelle
Lauren
Select Speed
1.0×
+
200 words per minute
Create a free account to unlock:
Bookmarks – save your favorite books
History – revisit books later
Ratings – rate books & see your ratings
Unlock unlimited listening
Your first week's on us!
Today: Get Instant Access
Listen to full summaries of 73,530 books. That's 12,000+ hours of audio!
Day 4: Trial Reminder
We'll send you a notification that your trial is ending soon.
Day 7: Your subscription begins
You'll be charged on Nov 22,
cancel anytime before.
Compare Features Free Pro
Read full text summaries
Summaries are free to read for everyone
Listen to summaries
12,000+ hours of audio
Unlimited Bookmarks
Free users are limited to 10
Unlimited History
Free users are limited to 10
What our users say
30,000+ readers
“...I can 10x the number of books I can read...”
“...exceptionally accurate, engaging, and beautifully presented...”
“...better than any amazon review when I'm making a book-buying decision...”
Save 62%
Yearly
$119.88 $44.99/yr
$3.75/mo
Monthly
$9.99/mo
Try Free & Unlock
7 days free, then $44.99/year. Cancel anytime.
Settings
Appearance