Lambda Expressions CSC 203 Christopher Siu 1 / 12 Lambda - - PowerPoint PPT Presentation

lambda expressions
SMART_READER_LITE
LIVE PREVIEW

Lambda Expressions CSC 203 Christopher Siu 1 / 12 Lambda - - PowerPoint PPT Presentation

Lambda Expressions CSC 203 Christopher Siu 1 / 12 Lambda Expressions A function is a mapping of inputs to outputs: input, x f Defjnition A lambda expression defjnes a function that can be passed and returned as though it were a value. Lambda


slide-1
SLIDE 1

Lambda Expressions

CSC 203 Christopher Siu

1 / 12

slide-2
SLIDE 2

Lambda Expressions

A function is a mapping of inputs to outputs: input, x

f

  • utput, f (x)

Defjnition

A lambda expression defjnes a function that can be passed and returned as though it were a value. Lambda expressions are also called anonymous functions, because they are not bound to one particular name. Java does not allow standalone functions — every method must be contained within a class.

2 / 12

slide-3
SLIDE 3

Lambda Expressions

Example

Consider the following class:

1

import java.util.Comparator;

2 3

public class PointComparator

4

implements Comparator<Point>

5

{

6

public int compare(Point pt1, Point pt2) {

7

return ((Double)pt1.getX())

8

.compareTo(pt2.getX());

9

}

10

}

A Comparator class is boilerplate that allows a single method to be passed around.

3 / 12

slide-4
SLIDE 4

Functional Interfaces

Defjnition

A functional interface defjnes exactly one abstract method. Functional interfaces can be targets for lambda expressions. A lambda expression in Java has the form: (PARAMETERS …) -> VALUE

Parameter and return types are omitted. If the method has one parameter, the parentheses are optional.

Example

1

Comparator<Point> compareByY =

2

(pt1, pt2) -> ((Double)pt1.getY())

3

.compareTo(pt2.getY());

4 / 12

slide-5
SLIDE 5

Functional Interfaces

In order to use a lambda in Java, an appropriate functional interface must already have been defjned.

Example

1

@FunctionalInterface

2

public interface PointStringifier

3

{

4

public String stringify(Point pt);

5

}

Functional interfaces don’t truly solve the problem of lambdas in Java, they only hide it behind interfaces and generics. The java.util.function package defjnes functional interfaces for common forms of functions.

5 / 12

slide-6
SLIDE 6

The Function Interface

Defjnition

1

@FunctionalInterface

2

public interface Function<T, R>

3

{

4

R apply(T t);

5

}

A java.util.function.Function encapsulates a method that takes one argument and returns a value.

Example

1

Function<Point, String> stringifyPoint =

2

pt -> "(" + pt.getX() + ", " + pt.getY() + ")";

6 / 12

slide-7
SLIDE 7

The Predicate Interface

Defjnition

1

@FunctionalInterface

2

public interface Predicate<T>

3

{

4

boolean test(T t);

5

}

A java.util.function.Predicate encapsulates a method that takes one argument and returns true or false.

Example

1

Predicate<Point> inFirstQuadrant =

2

pt -> pt.getX() > 0 && pt.getY() > 0;

7 / 12

slide-8
SLIDE 8

The Consumer Interface

Defjnition

1

@FunctionalInterface

2

public interface Consumer<T>

3

{

4

void accept(T t);

5

}

A java.util.function.Consumer encapsulates a method that takes one argument and does not return anything.

Example

1

Consumer<Point> printX =

2

pt -> System.out.println("pt.x: " + s.getX());

8 / 12

slide-9
SLIDE 9

The Supplier Interface

Defjnition

1

@FunctionalInterface

2

public interface Supplier<T>

3

{

4

T get();

5

}

A java.util.function.Supplier encapsulates a method that takes no arguments and returns a value.

Example

1

Supplier<Point> createOrigin =

2

() -> new Point(0.0, 0.0);

9 / 12

slide-10
SLIDE 10

Method References

Example

Consider the following lambda:

1

Function<Point, String> stringifyPoint =

2

pt -> pt.toString();

This lambda expression merely calls an existing method. A method reference in Java has the form: CLASS::METHOD

Example

1

Function<Point, String> stringifyPoint =

2

Point::toString;

10 / 12

slide-11
SLIDE 11

Composing Lambda Expressions

Some functional interfaces provide additional methods for combining lambda expressions.

Example

1

Function<Point, String> stringifyPoint =

2

pt -> "(" + pt.getX() + ", " + pt.getY() + ")";

3

Function<String, String> shoutString =

4

String::toUpperCase;

5 6

Function<Point, String> shoutPoint =

7

shoutString.compose(stringifyPoint);

1 shoutPoint fjrst stringifjes the given Point. 2 It then converts the string representation to upper case.

11 / 12

slide-12
SLIDE 12

Composing Lambda Expressions

Example

1

Comparator<Point> compareByX =

2

(pt1, pt2) -> ((Double)pt1.getX())

3

.compareTo(pt2.getX());

4

Comparator<Point> compareByY =

5

(pt1, pt2) -> ((Double)pt1.getY())

6

.compareTo(pt2.getY());

7 8

Comparator<Point> compareByXthenY =

9

compareByX.thenComparing(compareByY);

1 compareByXthenY fjrst compares x-coordinates. 2 It then breaks any ties by comparing y-coordinates.

12 / 12