Lambda Expressions
An Introduction with Examples in Java |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
->
return
Statement:
return
statement is not an
expression, it must be within enclosing curly brackets
(i.e., in a block)return
:
void
invocationsfinal
or (since Java v8)
are effectively final/** * A partial implementation of a utility class for performing vector arithmetic. * * @author Prof. David Bernstein, James Madison University * @version 1.0 */ public class VectorMath { /** * The requirements of a Metric. */ public interface Metric { /** * Calculate the distance between two vectors. * * @param v One vector * @param w The other vector * @return The distance between v and w */ public abstract double distance(double[] v, double[] w); } /** * Find the closest vector in a collection of destinations to the given * origin vector. * * @param d The Metric to use to calculate the distance * @param origin The origin vector * @param destinations The set of destination vectors * @return A reference to the closes vector in the colelction of destinations */ public static double[] closest(Metric d, double[] origin, double[]... destinations) { double min = Double.POSITIVE_INFINITY; double[] argmin = null; for (double[] dest: destinations) { double temp = d.distance(origin, dest); if (temp < min) { min = temp; argmin = dest; } } return argmin; } }
double[] nearby = VectorMath.closest( // A lambda expression for the rectilinear metric (double[] p, double[] q) -> { double sum = 0.0; for (int i=0; i<p.length; i++) { sum += Math.abs(p[i] - q[i]); } return sum; }, a, b, c);
double[] nearby = VectorMath.closest( // A lambda expression for the Euclidean metric (double[] p, double[] q) -> { double sum = 0.0; for (int i=0; i<p.length; i++) { sum += Math.pow(p[i] - q[i], 2.0); } return Math.sqrt(sum); }, a, b, c);
@Test public void sqrt() { Throwable exception = assertThrows(IllegalArgumentException.class, () -> { RealMath.sqrt(-1.0); }); assertEquals("The result is not real", exception.getMessage()); }
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() );