- Forward


Lambda Expressions
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • Review:
    • Local classes allow for the inclusion of classes that are tightlty coupled to a black of code
    • Anonymous classes allow for the inclusion of classes that are only used once and, hence, needn't be named
  • Lambda Expressions:
    • Provide a compact syntax for the equivalent of an anonymous class with only one method
Syntax
Back SMYC Forward
  • Formal Parameters:
    • A comma-separated list enclosed in parentheses (if there is more than one), with or without type declarations
  • The Arrow Token:
    • ->
  • The Body:
    • A single experssion or block statement
Details About the Body
Back SMYC Forward
  • A Single Expression:
    • The expression is evaluated and the result is returned
  • A Block with a return Statement:
    • Since the return statement is not an expression, it must be within enclosing curly brackets (i.e., in a block)
  • A Block without a return:
    • Is appropriate for void invocations
Common Uses
Back SMYC Forward
  • Compact Versions of Anonymous Classes:
    • An anonymous class would work but seems needlessly verbose
  • Method Passing:
    • You want to pass code as data (as in the command pattern and strategy pattern)
Access
Back SMYC Forward
  • Members in the Enclosing Class:
    • The local class can access all members
  • Access to Local Variables:
    • The local class can access all local variables that are declared to be final or (since Java v8) are effectively final
An Example with a Traditional Interface
Back SMYC Forward
javaexamples/nested/ComparatorExample.java (Fragment: lambda)
 
An Example with a Nested Interface
Back SMYC Forward
The Utility Class and Nested Interface
javaexamples/nested/VectorMath.java
 
An Example with a Nested Interface (cont.)
Back SMYC Forward
Using the Utility Class with One Lambda Expression
javaexamples/nested/VectorMathDriver.java (Fragment: rectilinear)
 
An Example with a Nested Interface (cont.)
Back SMYC Forward
Using the Utility Class with Another Lambda Expression
javaexamples/nested/VectorMathDriver.java (Fragment: Euclidean)
 
A JUnit Example
Back SMYC Forward
@Test public void sqrt() { Throwable exception = assertThrows(IllegalArgumentException.class, () -> { RealMath.sqrt(-1.0); }); assertEquals("The result is not real", exception.getMessage()); }
A GUI Example
Back SMYC Forward
JButton close = new JButton("Close"); // The most complete version close.addActionListener( (ActionEvent event) -> {f.dispose();} );
JButton close = new JButton("Close"); // Because the method is void it needn't be in a block close.addActionListener( (ActionEvent event) -> f.dispose() );
JButton close = new JButton("Close"); // The type of the parameter can be omitted close.addActionListener( (event) -> f.dispose() );
JButton close = new JButton("Close"); // Because there is one parameter the parentheses can be omitted close.addActionListener( event -> f.dispose() );
There's Always More to Learn
Back -