Java 8 in Action: Lambdas, Streams, and functional-style programming

Java 8 in Action: Lambdas, Streams, and functional-style programming

Raoul-Gabriel Urma, Mario Fusco, Alan Mycroft

Language: English

Pages: 424

ISBN: 1617291994

Format: PDF / Kindle (mobi) / ePub

Java 8 in Action: Lambdas, Streams, and functional-style programming

Raoul-Gabriel Urma, Mario Fusco, Alan Mycroft

Language: English

Pages: 424

ISBN: 1617291994

Format: PDF / Kindle (mobi) / ePub


Summary

Java 8 in Action is a clearly written guide to the new features of Java 8. The book covers lambdas, streams, and functional-style programming. With Java 8's functional features you can now write more concise code in less time, and also automatically benefit from multicore architectures. It's time to dig in!

Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.

About the Book

Every new version of Java is important, but Java 8 is a game changer. Java 8 in Action is a clearly written guide to the new features of Java 8. It begins with a practical introduction to lambdas, using real-world Java code. Next, it covers the new Streams API and shows how you can use it to make collection-based code radically easier to understand and maintain. It also explains other major Java 8 features including default methods, Optional, CompletableFuture, and the new Date and Time API.

This book is written for programmers familiar with Java and basic OO programming.

What's Inside

  • How to use Java 8's powerful new features
  • Writing effective multicore-ready applications
  • Refactoring, testing, and debugging
  • Adopting functional-style programming
  • Quizzes and quick-check questions

About the Authors

Raoul-Gabriel Urma is a software engineer, speaker, trainer, and PhD candidate at the University of Cambridge. Mario Fusco is an engineer at Red Hat and creator of the lambdaj library. Alan Mycroft is a professor at Cambridge and cofounder of the Raspberry Pi Foundation.

Table of Contents

    PART 1 FUNDAMENTALS
  1. Java 8: why should you care?
  2. Passing code with behavior parameterization
  3. Lambda expressions
  4. PART 2 FUNCTIONAL-STYLE DATA PROCESSING
  5. Introducing streams
  6. Working with streams
  7. Collecting data with streams
  8. Parallel data processing and performance
    PART 3 EFFECTIVE JAVA 8 PROGRAMMING
  9. Refactoring, testing, and debugging
  10. Default methods
  11. Using Optional as a better alternative to null
  12. CompletableFuture: composable asynchronousprogramming
  13. New Date and Time API
  14. PART 4 BEYOND JAVA 8
  15. Thinking functionally
  16. Functional programming techniques
  17. Blending OOP and FP: comparing Java 8 and Scala
  18. Conclusions and where next for Java
  19. APPENDIXES
  20. Miscellaneous language updates
  21. Miscellaneous library updates
  22. Performing multiple operations in parallelon a stream
  23. Lambdas and JVM bytecode

The New Digital Age: Reshaping the Future of People, Nations and Business

Understanding Mems: Principles and Applications

Getting Started with Adafruit FLORA: Making Wearables with an Arduino-Compatible Electronics Platform

High Techne: Art and Technology from the Machine Aesthetic to the Posthuman

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Heavy apples typically have a weight greater than 150 g.” Wearing your software engineering hat, you realize in advance that the farmer may want to vary the weight, so you create the following method to cope with various weights through an additional parameter: public static List filterApplesByWeight(List inventory, int weight) { List result = new ArrayList<>(); For (Apple apple: inventory){ if ( apple.getWeight() > weight ){ result.add(apple); } 39 } return result; } This

valid because the lambda () -> {} has the signature () -> void, which matches the signature of the abstract method run defined in Runnable. Note that running this code will do nothing because the body of the lambda is empty! 65 The second example is also valid. Indeed, the return type of the method fetch is Callable. Callable essentially defines a method with the signature () -> String when T is replaced with String. Because the lambda () -> "Tricky example ;-)" has the

menu.stream().count(); but the counting collector can be especially useful when used in combination with other collectors, as we demonstrate later. In the rest of this chapter, we assume that you’ve imported all the static factory methods of the Collectors class with import static java.util.stream.Collectors.*; so you can write counting() instead of Collectors.counting() and so on. Let’s continue exploring simple predefined collectors by looking at how you can find the maximum and minimum values

used to encode the same operation. This results in the following: Logically, this reduction operation proceeds as shown in figure 6.3, where an accumulator, initialized with a starting value, is iteratively combined, using an aggregating function, with the result of the application of the transforming function on each element of the stream. Figure 6.3. The reduction process calculating the total number of calories in the menu The counting collector we mentioned at the beginning of section 6.2

the first few lines before cat or tr has finished. A more mechanical analogy is a car-manufacturing assembly line where a stream of cars is queued between processing stations that each take a car, modify it, and pass it on to the next station for further processing; processing at separate stations is typically concurrent even though the assembly line is physically a sequence. 3 Purists will say a “stream of characters,” but it’s conceptually simpler to think that sort reorders lines. Figure

Download sample

Download