Skip to main content
Top

2015 | Book

Java Closures and Lambda

insite
SEARCH

About this book

Java Closures and Lambda introduces you to significant new changes to the Java language coming out of what is termed Project Lambda. These new changes make their debut in Java 8, and their highlight is the long-awaited support for lambda expressions in the Java language. You’ll learn to write lambda expressions and use them to create functional interfaces and default methods for evolving APIs, among many other uses.

The changes in Java 8 are significant. Syntax and usage of the language are changed considerably with the introduction of closures and lambda expressions. This book takes you through these important changes from introduction to mastery. Through a set of clear examples, you’ll learn to refactor existing code to take advantage of the new language features. You’ll learn what those features can do for you, and when they are best applied. You’ll learn to design and write new code having these important new features in mind from the very beginning.

Clearly explains the fantastic benefits resulting from Project Lambda Explains the syntax and IDE support for the new features Shows how to streamline your code by bringing some of the benefits of functional programming to the Java language Illustrates parallelism in closures through Stream and Spliterator objects Explains API evolution by adding methods to existing interfaces without breaking existing interface implementations, a technique addressing potential multiple inheritance issues

Table of Contents

Frontmatter
Chapter 1. Java 8: It’s a Whole New Java
Abstract
This book is about lambdas (closures) in Java 8. More than that, though, it’s about the new language that Java has become. The revolution was not televised, and it was met with little fanfare, but it has happened. It is possible for you to continue to write the same old Java code in our new Java 8 world, but unless you get onboard with the revolution, you will increasingly discover code that is called “Java,” but which has language, syntax, and customs that are foreign to you.
Robert Fischer
Chapter 2. Understanding Lambdas in Java 8
Abstract
The core new functionality in Java 8 is the introduction of lambdas. However, most introductions will show a couple of examples of lambdas, and then they leave it to the developer to figure out the rest. If you are going to be developing modern Java programs, however, you are going to need to be well versed in how lambdas can be used. Throughout this chapter, we will dig into the particularities of the syntax and semantics of lambdas, including how to create more complex lambdas, how to work with method calls and lambdas, and what this all looks like under the hood. We will also look at some classic ways to manipulate lambdas, and how they are implemented within Java. By the end of this chapter, you should be fluent in how to create the lambdas that you want.
Robert Fischer
Chapter 3. Lambda’s Domain: Collections, Maps, and Streams
Abstract
In the last chapter, we saw how lambdas themselves work, but we explicitly focused on lambdas without creating a context for their use. We extracted lambdas from their natural environment in order to analyze them under a microscope. In this chapter, we will begin to see lambdas in the wild.
Robert Fischer
Chapter 4. I/O with Lambdas
Abstract
So far, our exploration of lambdas and streams has existed in the well-controlled world of code. Sometimes, however, your code has to talk to the outside world. This chapter is where we enter into lambdas in the real world: instead of showing what is possible with lambdas in the abstract, we are getting into what programming with lambdas looks like in the grittiness of real code. This is a world where you end up having to work around a lot of unfortunate legacy code in Java, and where the nice theory and simple examples of lambdas give way to some of the gritty complexity. The important part for you is to understand how to work with that complexity in order to create a nicer development experience and more productive, stable code.
Robert Fischer
Chapter 5. Data Access with Lambdas
Abstract
In the last chapter, we saw how lambdas enable us to work with files and streams. In this chapter, we will look at interacting with the database. Way back in chapter 1, we saw how lambdas can help Spring’s JDBCTemplate by making it easy to implement its callback interfaces. In chapter 3, we were introduced to streams, and in chapter 4, we saw how streams can make it easier to work with file data. In this chapter, we will see the specifics of working with Java’s data access structures (the java.sql package). This will give us an opportunity to meet another new data structure in Java 8’s standard library: the spliterator.
Robert Fischer
Chapter 6. Lambda Concurrency
Abstract
The Java programming language is, at its core, a series of instructions. These instructions are executed in sequence. By default, you only ever have one of these series going at a time. However, if you have multiple processing cores on your machine, or if you have any kind of I/O, then this single series of instructions suddenly becomes very inefficient. If your program has a lot to do, but its single stream has to wait for the “read” I/O instruction to complete before doing anything else, then your program is wasting time. If you have four processing cores, but only one stream of instructions, then you are at best only using a quarter of your potential power. The solution to this is to have multiple streams of instructions executing at the same time within your program, which is called concurrency.
Robert Fischer
Chapter 7. Lambdas and Legacy Code
Abstract
At this point in this book, you have seen all the wonderful things that lambdas can do for your codebase. But what about the huge pile of legacy code that is out there? There is a lot of code to be merged into this new reality, and a lot of Java developers are commited to projects that have to drag forward those balls of mud. How can lambdas make their life easier?
Robert Fischer
Chapter 8. Lambdas in Java Bytecode
Abstract
This chapter is for those who want to go deeper and understand how lambdas really work. This is your red pill vs. blue pill moment: do you want to live in a world where Java has lambdas and they work great and everything is slick and awesome, or do you want to see what is really going on beneath the surface and discover the magic of the compiler pulls? If you are happy with the lambdas as you know and love them, then skip this chapter and enjoy. If you need to know what your code really means, then hold on tight: it’s time to go down the rabbit hole.
Robert Fischer
Appendix A. A Tour of Paradigms
Abstract
Many books, articles, and conversations will wax poetic about programming language paradigms, comparing and contrasting “imperative,” “object-oriented,” and “functional” without explaining what they are. Worse, when people go to explain these particular styles, the result is often rather vague. This appendix will provide an overview of the three dominant paradigms in post-functional programming by implementing the same functionality three different ways: we will use a database set-up process as a common problem, and show how you would solve that problem in each of the different paradigms. All of this will be done directly from within Java, so you can see how Java looks when forced to be strongly in one single paradigm. At the end of the chapter, we will highlight some additional programming paradigms and styles so that you are at least familiar with the terms.
Robert Fischer
Backmatter
Metadata
Title
Java Closures and Lambda
Author
Robert Fischer
Copyright Year
2015
Publisher
Apress
Electronic ISBN
978-1-4302-5999-2
Print ISBN
978-1-4302-5998-5
DOI
https://doi.org/10.1007/978-1-4302-5999-2

Premium Partner