Introduction - Mastering Lambdas: Java Programming in a Multicore World (2015)

Mastering Lambdas: Java Programming in a Multicore World (2015)

Introduction

The changes in Java 8 are the biggest in the history of the language. They promise to alter Java programming toward a functional style will help you to write code that is more concise and expressive, and (in many cases) ready to take advantage of parallel hardware. In this book, you will discover how the seemingly minor changes that introduce lambda expressions make this possible. You will learn how lambda expressions enable you to write a Java function in a single line of code, and how to use this capability to program the new Stream API, condensing verbose collection processing code into simple and readable stream programs. Studying the mechanisms that create and consume streams will lead to an analysis of their performance and enable you to judge when to invoke the parallel execution features of the API.

Lastly, integrating the new features into the existing Java platform libraries required the evolution of existing collection interfaces, previously prevented by compatibility problems. You will learn about how default methods solve these problems and how to use them in evolving your own APIs.

Chapter 1: Taking Java to the Next Level

This chapter sets the scene for the introduction of both lambda expressions and streams to Java, motivating the changes by the need both for better programming idioms and for Java to begin providing support for multicore processors.

Chapter 2: The Basics of Java Lambda Expressions

This chapter covers the syntax of lambda expressions, how and where you can use them, how they differ from anonymous inner classes, and the convenient shorthand provided by method and constructor references.

Chapter 3: Introduction to Streams and Pipelines

This chapter explains the stream lifecycle and the basis of programming with streams, with collection processing providing examples of stream sources and of intermediate and terminal operations.

Chapter 4: Ending Streams: Collection and Reduction

This chapter gives a detailed view of terminal operations, and in particular how stream elements can be accumulated into collections using mutable reduction. The examples of Chapter 3 are extended using collectors, the library implementations of mutable reduction. We will see when it is necessary to go beyond the library implementations and how to write your own collector.

Chapter 5: Starting Streams: Sources and Spliterators

This chapter examines ways of starting streams, both using library classes and, when necessary, writing your own spliterator. The problem of exception handling in stream programming is explored in depth. An extended example shows the flexibility of the model by reimplementing various options of grep by means of stream processing.

Chapter 6: Stream Performance

This chapter offers ways of understanding the relative performance of stream processes executing in parallel in terms of the splitting efficiency of their sources, the workload of their intermediate operations, and the concurrency of their terminal operations. Microbenchmarks are introduced as a way of measuring stream performance and, using them, the programs developed elsewhere in the book are analyzed.

Chapter 7: API Evolution with Default Methods

This chapter explains how the introduction of default methods solves long-standing problems in Java programming, and in particular how they make it possible, for the first time, for interface-based Java APIs to evolve. It also covers the use of static interface methods.

Intended Audience

This book is aimed at Java developers comfortable with any version from Java 5 onward who have heard about the exciting changes in Java 8 and want to know about them. You do not need to have met lambda expressions or closures in any other language, or to have any experience with functional programming. (If you have, so much the better, of course.)

The book doesn’t assume familiarity with the platform libraries except for the standard collections of the Java Collections Framework; if you don’t know them well, be prepared to consult the Javadoc sometimes.

A few sections contain more advanced material: these are introduced as being suitable for a second reading.