Chapter 3. Lambda expressions

 

This chapter covers

  • Lambdas in a nutshell
  • Where and how to use lambdas
  • The execute-around pattern
  • Functional interfaces, type inference
  • Method references
  • Composing lambdas

In the previous chapter, you saw that passing code with behavior parameterization is useful for coping with frequent requirement changes in your code. It lets you define a block of code that represents a behavior and then pass it around. You can decide to run that block of code when a certain event happens (for example, a button click) or at certain points in an algorithm (for example, a predicate such as “only apples heavier than 150 g” in the filtering algorithm or the customized comparison operation in sorting). In general, using this concept you can write code that’s more flexible and reusable.

But you saw that using anonymous classes to represent different behaviors is unsatisfying. It’s verbose, which doesn’t encourage programmers to use behavior parameterization in practice. In this chapter, we’ll teach you about a new feature in Java 8 that tackles this problem: lambda expressions. They let you represent a behavior or pass code in a concise way. For now you can think of lambda expressions as anonymous functions, methods without declared names, but which can also be passed as arguments to a method as you can with an anonymous class.

3.1. Lambdas in a nutshell

 
 

3.2. Where and how to use lambdas

 
 
 

3.3. Putting lambdas into practice: the execute-around pattern

 
 
 

3.4. Using functional interfaces

 
 
 

3.5. Type checking, type inference, and restrictions

 
 

3.6. Method references

 
 
 

3.7. Putting lambdas and method references into practice

 

3.8. Useful methods to compose lambda expressions

 
 
 

3.9. Similar ideas from mathematics

 
 

Summary

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest