Prerequisites To run the examples from download section, you have to download the latest version of JDK 8 and Eclipse Luna 4.4.0 or upper.  Java Development Kit 8 (JDK8) Early AccessRecommended: Download the JDK 8 API Docs from the same page.Eclipse Luna. Version 4.4.0  includes support for JDK 8. Lambda expressions.Lambda expressions are part of Java Platform Standard Edition 8 (Java SE 8). They provide a clean way  to represent one method interface using an expression. Lambda expressions also brings improvements to the Collection libraries making it easier to iterate through, filter, and extract data from a Collection. Functional Interfaces What they are ?  The code that defines the ActionListener interface, looks something like this: 1 package java.awt.event; 2 import java.util.EventListener; 3 4 public interface ActionListener extends EventListener { 5 6 public void actionPerformed(ActionEvent e); 7 8 } The ActionListener example is an interface with only one method. With Java SE 8, an interface that follows this pattern is known as a functional interface. Note: This type of interface, was previously known as a Single Abstract Method type (SAM).Using functional interfaces with anonymous inner classes are a common pattern in Java. In addition to the EventListener classes, interfaces like Runnable and Comparator are used in a similar manner. Now, functional interfaces are leveraged for use with lambda expressions. Lambda Expression Syntax Lambda expressions permits to compress code converting five lines of code into a single statement. This simple horizontal solution solves the "vertical problem" presented by inner classes. A lambda expression is composed, in general,  of three parts. Argument ListArrow TokenBody (int x, int y)->x + y The body can be either a single expression or a statement block. In the expression form, the body is simply evaluated and returned. In the block form, the body is evaluated like a method body and a return statement returns control to the caller of the anonymous method.  Take a look at these examples: (int x, int y) -> x + y () -> 42 (String s) -> { System.out.println(s); } The first expression takes two integer arguments, named x and y, and uses the expression form to return x+y. The second expression takes no arguments and uses the expression form to return an integer 42. The third expression takes a string and uses the block form to print the string to the console, and returns nothing. With the basics of syntax covered, let's look at some examples. The lambda expression, converts five lines of code into one statement. Listener with Lambda. java.util.function.* The package java.util.function.* present a huge quantity of  Functional interfaces providing target types for lambda expressions and method references. From the package we will examine: Predicate and Function. In the first case a Predicate, based on a range of age,  will be made to filter a collection of a Person objects; in the second case a Function returning a simple string will be applied to a method of Person class, in a foreach statement.
Java EEJDK 8Lambda expressions Functional expressions Eclipse
written by Ivan Cerrato
back to home
[if gte mso 9]> 0 0 1 40 232 extech 1 1 271 14.0 Normal 0 false false false EN-US JA X-NONE