Java Closures and Lambda

Java Closures and Lambda

Language: English

Pages: 220

ISBN: 1430259981

Format: PDF / Kindle (mobi) / ePub

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 What you'll learn Encapsulate key bits of logic into anonymous functions without the need for anonymous classes Define closures to be used as generators of comparison functions Define map, filter, and reduce functions that are useful in working with lists and other collections Trap and handle exceptions involving lambdas and closures, including the passing of exception-type parameters Anticipate and provide for concurrency so that your code successfully scales across multiple CPU co


















like Listing 1-1. The tech lead saw that code and was not impressed, and so he rewrote it into Listing 1-2, which was longer, but both more readable and more efficient. Listing 1-1.  Original Implementation of cloneWithoutNulls(List) public static List cloneWithoutNulls(final List list) { List out = new ArrayList(list); while(out.remove(null)) {} return out; }  Listing 1-2.  Refined Implementation of cloneWithoutNulls(List) public static List cloneWithoutNulls(final List

is where we will really get into a fancy Stream stunt, and the real power of a Stream will come out. The difficulty is that we have a fairly complex operation on a stream. We want to handle and then remove elements from the stream meeting a certain criteria: in this case, we want to handle and remove Result instances that contain exceptions. If you glance over the Stream API, it first seems like we want to perform a map operation: we want to take in types of Result and return a String,

#0:accept:(Ljava/io/PrintStream;)Ljava/util/ function/Consumer; 13: areturn } If we compare this to the function in 8-2, we see that there is a bit more going on. The first thing is the getstatic call, which loads System.out onto the stack. Eventually, we will pass that instance into our invokedynamic call on instruction 8, which will again go by way of the bootstrap method to produce our Consumer. The bootstrap method is practically identical to what we saw with the static method reference,

acted on that data. Both of these problems are addressed by changing the underlying model of execution from imperative to functional: that is, instead of thinking of executing a series of steps, you think about applying transformations and mappings. This gave rise to the post-functional programming paradigm. Post-functional programming makes it much easier to reason about your object-oriented application. By strongly limiting mutability and focusing on composing small transformations, we can

lambda with zero arguments is strange: how do you represent the absence of any arguments? Simply using the right arrow without any arguments would lead to many ambiguous cases in the Java syntax, so that is not an option. Instead, they introduced a placeholder for zero-argument functions, just like numbers have a placeholder symbol for zero. That placeholder is a left parenthesis immediately followed by a right parenthesis, which even looks like zero: (). An example of using it is in Listing 2-8;

Download sample