Chapter 19. Lambda Expressions

Lamda Expressions (λEs), also known as closures, provides a means to represent anonymous methods. Supported by Project Lambda, λEs allow for the creation and use of single method classes. These methods have a basic syntax which provide for the omission of modifiers, the return type and optional parameters. The specification for λEs is set out in JSR 335 which is divided into 7 parts; Functional Interfaces, Lambda Expressions, Method & Contstructor References, Poly Expressions, Typing & Evaluation, Type Interence andd Default Methods. This chapter focuses on the first two.

λEs Basics

λEs must have a functional interface (FI). An FI is an interface that has one abstract method and may have zero or more default methods. FIs provide target types for lambda expressions and method references and ideally should be annotated with @FunctionalInterface to aid the developer and compiler with design intent.

@FunctionalInterface
public interface Comparator<T> {
  // Only one abstract method allowed
  int compare(T o1, T o2);
  // Overriding allowed
  boolean equals(Object obj);
  // Optional default methods allowed
}

λEs Syntax and Example

The general usage of Lambda Expressions include a parameter list, a return type and a body.

(parameter list) -> { statements; }

Examples of λEs include:

() -> 66
(x,y) -> x + y
(Integer x, Integer y) -> x*y
(String s) -> { System.out.println(s); }

This simple JavaFX GUI application adds text to the title bar once the button is pressed. The code makes use of the EventHandler functional interface with the one abstract method; handle().

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class JavaFxApp extends Application {
  @Override
  public void start(Stage stage) {
    Button b = new Button();
    b.setText("Press Button");
    // Anonymous inner class usage
    b.setOnAction(new EventHandler<ActionEvent>() {
      @Override
      public void handle(ActionEvent event) {
        stage.setTitle("Title");
      }
    });
    StackPane root = new StackPane();
    root.getChildren().add(b);
    Scene scene = new Scene(root, 200, 50);
    stage.setScene(scene);
    stage.show();
  }
  public static void main(String[] args) {
    launch();
  }
}

To refactor this anonymous inner class into a Lambda Expression, the parameter type needs to be either (ActionEvent event) or just (event) and the desired functionality needs to be provided as statements in the body.

// Lambda Expression usage
b.setOnAction((ActionEvent event) -> {
  stage.setTitle("λEs rock!");
});

Tip

Modern IDEs have features to convert anonymous inner classes to Lambda Expressions.

Reference Comparator Functional Interface to see another example of lambda expressions with the Comparator functional interface.

Method and Constructor References

A method reference refers to an existing method without invoking it. Types include static method reference, instance method of particular object, super method of particular object, and instance method of arbitrary object of particular type. Method references also include class constructor reference and array constructor reference.

"some text"::length  // Get length of String
String::length // Get length of String
CheckAcct::compareByBalance  // Static method ref
myComparator::compareByName // Inst method part obj
super::toString // Super method part object
String::compareToIgnoreCase // Inst method arb obj
ArrayList<String>::new  // New ArrayList constructor
Arrays::sort  // Sort array elements

Specific Purpose FIs

Annotated FIs listed in table Table 19-1 have been estasblished for specific purposes relative to the packages/APIs in which they are in. Not all functional interfaces in the Java SE API are currently annotated.

Table 19-1. Specific Purpose FIs
APIClassMethod

AWT

KeyEventDispacter

dispatchKeyEvent (KeyEvent e)

AWT

KeyEventPostProcessor

postProcessKeyEvent (KeyEvent e)

IO

FileFilter

FileFilter ()

IO

FilenameFilter

accept (File pathname)

LANG

Runnable

run ()

NIO

DirectorStream

iterator ()

NIO

PathMatcher

matches (Path path)

TIME

TemporalAdjuster

adjustInto (Temporal temporal)

TIME

TemporalQuery

queryFrom (TemporalAccessor temporal)

UTIL

Comparator

compare (T o1, T o2)

CONC

Callable

call ()

LOG

Filter

isLoggable (LogRecord record)

PREF

PreferenceChangeListener

preferenceChange (PreferenceChangeEvent evt)

General Purpose FIs

The java.util.function package is made up of general purpose FIs for the primary use of features of the JDK. Table Table 19-2 lists them all as there may be times where a developer may need to use them.

Table 19-2. Functional Interfaces Functional Package
Consumeraccept (T t)

BiConsumer

accept (T t, U u)

ObjDoubleConsumer

accept (T t, double value)

ObjIntConsumer

accept (T t, int value)

ObjLongConsumer

accept (T t, long value)

DoubleConsumer

accept (double value)

IntConsumer

accept (int value)

LongConsumer

accept (long value)

Function

apply (T t)

BiFunction

apply (T t, U u)

DoubleFunction

apply (double value)

IntFunction

apply (int value)

LongFunction

apply (long value)

BinaryOperator

apply (Object, Object)

ToDoubleBiFunction

applyAsDouble (T t, U u)

ToDoubleFunction

applyAsDouble (T value)

IntToDoubleFunction

applyAsDouble (int value)

LongToDoubleFunction

applyAsDouble(long value)

DoubleBinaryOperator

applyAsDouble (double left, double right)

ToIntBiFunction

applyAsInt (T t, U u)

ToIntFunction

applyAsInt (T value)

LongToIntFunction

applyAsInt (long value)

DoubleToIntFunction

applyAsInt(double value)

IntBinaryOperator

applyAsInt (int left, int right)

ToLongBiFunction

applyAsLong (T t, U u)

ToLongFunction

applyAsLong (T value)

DoubleToLongFunction

applyAsLong (double value)

IntToLongFunction

applyAsLong (int value)

LongBinaryOperator

applyAsLong (long left, long right)

BiPredicate

test (T t, U u)

Predicate

test (T t)

DoublePredicate

test (double value)

IntPredicate

test (int value)

LongPredicate

test (long value)

Supplier

get()

BooleanSupplier

getAsBoolean()

DoubleSupplier

getAsDouble()

IntSupplier

getAsInt()

LongSupplier

getAsLong()

UnaryOperator

identity()

DoubleUnaryOperator

identity()

IntUnaryOperator

applyAsInt (int operand)

LongUnaryOperator

applyAsInt (long value)

Resources for λEs

This section provides helful links to tutorials and community resources about λEs.

Community Resources

Online bulletin boards, mailing lists and instructional videos are all resources that provide support to learning and using λEs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.81.214