Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s...

43
Overview of Java 8 Lambda Expressions & Method References Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Transcript of Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s...

Page 1: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

Overview of Java 8 Lambda Expressions

& Method References

Douglas C. [email protected]

www.dre.vanderbilt.edu/~schmidt

Professor of Computer Science

Institute for Software

Integrated Systems

Vanderbilt University

Nashville, Tennessee, USA

Page 2: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

2

Learning Objectives in this Lesson• Recognize foundational functional

programming features in Java 8, e.g.,

• Lambda expressions

Page 3: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

3

Learning Objectives in this Lesson• Recognize foundational functional

programming features in Java 8, e.g.,

• Lambda expressions

• Method (& constructor) references

Page 4: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

4

Learning Objectives in this Lesson• Recognize foundational functional

programming features in Java 8, e.g.,

• Lambda expressions

• Method (& constructor) references

Several concise examples are used to showcase foundational Java 8 features

Page 5: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

5

Overview of Lambda Expressions

Page 6: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

6

• A lambda expression is an unnamed block of code (with optional parameters) that can be stored, passed around, & executed later

new Thread(() ->

System.out.println("hello world"))

.start();

Overview of Lambda Expressions

See www.drdobbs.com/jvm/lambda-expressions-in-java-8/240166764

Page 7: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

7

• A lambda expression is an unnamed block of code (with optional parameters) that can be stored, passed around, & executed later, e.g.,

new Thread(() ->

System.out.println("hello world"))

.start();

Overview of Lambda Expressions

This lambda expression takes no parameters, i.e., “()”

Page 8: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

8

• A lambda expression is an unnamed block of code (with optional parameters) that can be stored, passed around, & executed later, e.g.,

new Thread(() ->

System.out.println("hello world"))

.start();

Overview of Lambda Expressions

It defines a computation that will run in a separate Java thread

See docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

Page 9: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

9

• A lambda expression is an unnamed block of code (with optional parameters) that can be stored, passed around, & executed later, e.g.,

new Thread(() ->

System.out.println("hello world"))

.start();

Overview of Lambda Expressions

It defines a computation that will run in a separate Java thread

Runtimethread stack

See docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

Page 10: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

10

• A lambda expression is an unnamed block of code (with optional parameters) that can be stored, passed around, & executed later, e.g.,

new Thread(() ->

System.out.println("hello world"))

.start();

Runnable r = () -> System.out.println("hello world");

new Thread(r).start();

Overview of Lambda Expressions

See docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

You can also store a lambda expression into a variable & pass that variable to a method

Page 11: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

11

• A lambda expression is an unnamed block of code (with optional parameters) that can be stored, passed around, & executed later, e.g.,

new Thread(() ->

System.out.println("hello world"))

.start();

Overview of Lambda Expressions

Lambda expressions are compact since they just focus on computation(s) to perform

Page 12: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

12

• A lambda expression is an unnamed block of code (with optional parameters) that can be stored, passed around, & executed later, e.g.,

new Thread(() ->

System.out.println("hello world"))

.start();

vs

new Thread(new Runnable() {

public void run() {

System.out.println("hello world");

}}).start();

Overview of Lambda Expressions

Conversely, this anonymous inner class requires more code to write each time

Page 13: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

13See github.com/douglascraigschmidt/LiveLessons/tree/master/Java8/ex1

• Lambda expressions can work with multiple parameters in a more compact manner than anonymous inner classes

String[] nameArray = {"Barbara", "James", "Mary", "John",

"Robert", "Michael", "Linda", "james", "mary"};

Arrays.sort(nameArray, new Comparator<String>(){

public int compare(String s,String t) { return

s.toLowerCase().compareTo(t.toLowerCase()); }});

vs

Arrays.sort(nameArray,

(s, t) -> s.compareToIgnoreCase(t));

Overview of Lambda Expressions

Page 14: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

14

• Lambda expressions can work with multiple parameters in a more compact manner than anonymous inner classes, e.g.,

String[] nameArray = {"Barbara", "James", "Mary", "John",

"Robert", "Michael", "Linda", "james", "mary"};

Arrays.sort(nameArray, new Comparator<String>(){

public int compare(String s,String t) { return

s.toLowerCase().compareTo(t.toLowerCase()); }});

vs

Arrays.sort(nameArray,

(s, t) -> s.compareToIgnoreCase(t));

Overview of Lambda Expressions

List of names represented as strings

Page 15: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

15

• Lambda expressions can work with multiple parameters in a more compact manner than anonymous inner classes, e.g.,

String[] nameArray = {"Barbara", "James", "Mary", "John",

"Robert", "Michael", "Linda", "james", "mary"};

Arrays.sort(nameArray, new Comparator<String>(){

public int compare(String s,String t) { return

s.toLowerCase().compareTo(t.toLowerCase()); }});

Extraneous syntax for anonymous inner class

Overview of Lambda Expressions

Page 16: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

16

• Lambda expressions can work with multiple parameters in a more compact manner than anonymous inner classes, e.g.,

String[] nameArray = {"Barbara", "James", "Mary", "John",

"Robert", "Michael", "Linda", "james", "mary"};

Arrays.sort(nameArray, new Comparator<String>(){

public int compare(String s,String t) { return

s.toLowerCase().compareTo(t.toLowerCase()); }});

vs

Arrays.sort(nameArray,

(s, t) -> s.compareToIgnoreCase(t));

(s, t) is short for (String s, String t), which leverages Java 8’s type inference capabilities

Overview of Lambda Expressions

See docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html

Page 17: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

17

• Lambda expressions can work with multiple parameters in a more compact manner than anonymous inner classes, e.g.,

String[] nameArray = {"Barbara", "James", "Mary", "John",

"Robert", "Michael", "Linda", "james", "mary"};

Arrays.sort(nameArray, new Comparator<String>(){

public int compare(String s,String t) { return

s.toLowerCase().compareTo(t.toLowerCase()); }});

vs

Arrays.sort(nameArray,

(s, t) -> s.compareToIgnoreCase(t));

This lambda expression omits the method name & extraneous syntax

Overview of Lambda Expressions

Page 18: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

18

• A lambda expression can access (effectively) final variables from the enclosing scope

int answer = 42;

new Thread(() ->

System.out.println("The answer is " + answer))

.start();

Overview of Lambda Expressions

This lambda expression can access the value of “answer,” which is an effectively final variable whose value never changes after it’s initialized

See www.linkedin.com/pulse/java-8-effective-final-gaurhari-dass

Page 19: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

19

Overview of Method References

Page 20: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

20

• A method reference is a compact, easy-to-read handle for a method that already has a name

Overview of Method References

See docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html

Kind Example

1. Reference to a static method ContainingClass::staticMethodName

2. Reference to an instance method of a particular object

containingObject::instanceMethodName

3. Reference to an instance method of an arbitrary object of a given type

ContainingType::methodName

4. Reference to a constructor ClassName::new

Page 21: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

21

• A method reference is a compact, easy-to-read handle for a method that already has a name

Overview of Method References

Kind Example

1. Reference to a static method ContainingClass::staticMethodName

2. Reference to an instance method of a particular object

containingObject::instanceMethodName

3. Reference to an instance method of an arbitrary object of a given type

ContainingType::methodName

4. Reference to a constructor ClassName::new

Page 22: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

22

Overview of Method References• Method references are even more compact than anonymous inner classes &

lambda expressions

Page 23: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

23

• Method references are more compact than alternative mechanisms, e.g.,

String[] nameArray = {"Barbara", "James", "Mary", "John",

"Robert", "Michael", "Linda", "james", "mary"};

Arrays.sort(nameArray, new Comparator<String>(){

public int compare(String s,String t) { return

s.toLowerCase().compareTo(t.toLowerCase()); }});

vs

Arrays.sort(nameArray,

(s, t) -> s.compareToIgnoreCase(t));

vs

Arrays.sort(nameArray, String::compareToIgnoreCase);

Overview of Method References

Method reference is even more compact

Page 24: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

24

• Method references are more compact than alternative mechanisms, e.g.,

String[] nameArray = {"Barbara", "James", "Mary", "John",

"Robert", "Michael", "Linda", "james", "mary"};

Arrays.sort(nameArray, new Comparator<String>(){

public int compare(String s,String t) { return

s.toLowerCase().compareTo(t.toLowerCase()); }});

vs

Arrays.sort(nameArray,

(s, t) -> s.compareToIgnoreCase(t));

vs

Arrays.sort(nameArray, String::compareToIgnoreCase);

Overview of Method References

It’s good practice to use method references whenever you can!

Page 25: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

25

Applying Method References in Practice

Page 26: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

26

• Method references can be used to print a collection or array in various ways

String[] nameArray = {"Barbara", "James", "Mary", "John",

"Robert", "Michael", "Linda", "james", "mary"};

Applying Method References in Practice

List of names represented as strings

Page 27: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

27

• Method references can be used to print a collection or array in various ways

String[] nameArray = {"Barbara", "James", "Mary", "John",

"Robert", "Michael", "Linda", "james", "mary"};

• System.out.println() can be used to print out an array

System.out.println(Arrays.asList(nameArray));

prints

[Barbara, James, Mary, John, Linda, Michael, Linda, james, mary]

See docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html#println

Applying Method References in Practice

Page 28: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

28

• Method references can be used to print a collection or array in various ways

String[] nameArray = {"Barbara", "James", "Mary", "John",

"Robert", "Michael", "Linda", "james", "mary"};

• System.out.println() can be used to print out an array

System.out.println(Arrays.asList(nameArray));

prints

[Barbara, James, Mary, John, Linda, Michael, Linda, james, mary]

See www.tutorialspoint.com/java/util/arrays_aslist.htm

Applying Method References in Practice

Returns a fixed-size list backed by the specified array

Page 29: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

29

• Method references can be used to print a collection or array in various ways

String[] nameArray = {"Barbara", "James", "Mary", "John",

"Robert", "Michael", "Linda", "james", "mary"};

• System.out.println() can be used to print out an array

• Java 8’s forEach() method can be used to print out values of an array

See www.javaworld.com/article/2461744/java-language/java-language-iterating-over-collections-in-java-8.html

Applying Method References in Practice

Page 30: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

30

• Method references can be used to print a collection or array in various ways

String[] nameArray = {"Barbara", "James", "Mary", "John",

"Robert", "Michael", "Linda", "james", "mary"};

• System.out.println() can be used to print out an array

• Java 8’s forEach() method can be used to print out values of an array, e.g.

• In conjunction with a stream & method reference

Stream.of(nameArray).forEach(System.out::print);

prints

BarbaraJamesMaryJohnLindaMichaelLindajamesmary

See docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#of

Factory method that creates a stream from an array

Applying Method References in Practice

Page 31: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

31

• Method references can be used to print a collection or array in various ways

String[] nameArray = {"Barbara", "James", "Mary", "John",

"Robert", "Michael", "Linda", "james", "mary"};

• System.out.println() can be used to print out an array

• Java 8’s forEach() method can be used to print out values of an array, e.g.

• In conjunction with a stream & method reference

Stream.of(nameArray).forEach(System.out::print);

prints

BarbaraJamesMaryJohnLindaMichaelLindajamesmary

See docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#forEach

Performs method reference action on each stream element

Applying Method References in Practice

Page 32: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

32

• Method references can be used to print a collection or array in various ways

String[] nameArray = {"Barbara", "James", "Mary", "John",

"Robert", "Michael", "Linda", "james", "mary"};

• System.out.println() can be used to print out an array

• Java 8’s forEach() method can be used to print out values of an array, e.g.

• In conjunction with a stream & method reference

• In conjunction with a collection (e.g., List)

Arrays.asList(nameArray).forEach(System.out::print);

prints

BarbaraJamesMaryJohnLindaMichaelLindajamesmary

Converts array into a List

See docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList

Applying Method References in Practice

Page 33: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

33

• Method references can be used to print a collection or array in various ways

String[] nameArray = {"Barbara", "James", "Mary", "John",

"Robert", "Michael", "Linda", "james", "mary"};

• System.out.println() can be used to print out an array

• Java 8’s forEach() method can be used to print out values of an array, e.g.

• In conjunction with a stream & method reference

• In conjunction with a collection (e.g., List)

Arrays.asList(nameArray).forEach(System.out::print);

prints

BarbaraJamesMaryJohnLindaMichaelLindajamesmary

See docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html#forEach

Applying Method References in Practice

Performs method reference action on each list element

Page 34: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

34

• Method references can be used to print a collection or array in various ways

String[] nameArray = {"Barbara", "James", "Mary", "John",

"Robert", "Michael", "Linda", "james", "mary"};

• System.out.println() can be used to print out an array

• Java 8’s forEach() method can be used to print out values of an array, e.g.

• In conjunction with a stream & method reference

• In conjunction with a collection (e.g., List)

• forEach() on a stream differs slight from forEach() on a collection

See stackoverflow.com/a/23232560

Applying Method References in Practice

Page 35: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

35

• Method references can be used to print a collection or array in various ways

String[] nameArray = {"Barbara", "James", "Mary", "John",

"Robert", "Michael", "Linda", "james", "mary"};

• System.out.println() can be used to print out an array

• Java 8’s forEach() method can be used to print out values of an array, e.g.

• In conjunction with a stream & method reference

• In conjunction with a collection (e.g., List)

• forEach() on a stream differs slight from forEach() on a collection

• e.g., forEach() ordering is undefined on a stream, whereas it’s defined for a collection

See stackoverflow.com/a/23232560

Applying Method References in Practice

Page 36: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

36

Implementing Closureswith Lambda Expressions

Page 37: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

37See github.com/douglascraigschmidt/LiveLessons/tree/master/Java8/ex1

• Lambda expressions can implement (simplified) variants of “closures”

• A closure is a method that has an environment w/at least 1 bound variable

class ClosureExample {

private int mRes;

Thread makeThreadClosure(String s, int n) {

return new Thread(() ->

System.out.println(s + (mRes += n)));

}

ClosureExample() throw InterruptedException {

Thread t = makeThreadClosure("result = ", 10);

t.start(); t.join();

}

}

Implementing Closures with Lambda Expressions

Page 38: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

38

• Lambda expressions can implement (simplified) variants of “closures”

• A closure is a method that has an environment w/at least 1 bound variable

class ClosureExample {

private int mRes;

Thread makeThreadClosure(String s, int n) {

return new Thread(() ->

System.out.println(s + (mRes += n)));

}

ClosureExample() throw InterruptedException {

Thread t = makeThreadClosure("result = ", 10);

t.start(); t.join();

}

}

Implementing Closures with Lambda Expressions

See en.wikipedia.org/wiki/Closure_(computer_programming)

Page 39: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

39A bound variable is name that has a value, such as a number

• Lambda expressions can implement (simplified) variants of “closures”

• A closure is a method that has an environment w/at least 1 bound variable

class ClosureExample {

private int mRes;

Thread makeThreadClosure(String s, int n) {

return new Thread(() ->

System.out.println(s + (mRes += n)));

}

ClosureExample() throw InterruptedException {

Thread t = makeThreadClosure("result = ", 10);

t.start(); t.join();

}

}

Implementing Closures with Lambda Expressions

This private field & method params are bound variables

Page 40: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

40

• Lambda expressions can implement (simplified) variants of “closures”

• A closure is a method that has an environment w/at least 1 bound variable

class ClosureExample {

private int mRes;

Thread makeThreadClosure(String s, int n) {

return new Thread(() ->

System.out.println(s + (mRes += n)));

}

ClosureExample() throw InterruptedException {

Thread t = makeThreadClosure("result = ", 10);

t.start(); t.join();

}

}

Implementing Closures with Lambda Expressions

This lambda implements a closure that captures a private field & method params

See bruceeckel.github.io/2015/10/17/are-java-8-lambdas-closures

Page 41: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

41

• Lambda expressions can implement (simplified) variants of “closures”

• A closure is a method that has an environment w/at least 1 bound variable

class ClosureExample {

private int mRes;

Thread makeThreadClosure(String s, int n) {

return new Thread(() ->

System.out.println(s + (mRes += n)));

}

ClosureExample() throw InterruptedException {

Thread t = makeThreadClosure("result = ", 10);

t.start(); t.join();

}

}

Implementing Closures with Lambda Expressions

Values of private fields can be updated in a lambda, but not params or local vars (which are read-only)

See dzone.com/articles/java-8-lambas-limitations-closures

Page 42: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

42

• Lambda expressions can implement (simplified) variants of “closures”

• A closure is a method that has an environment w/at least 1 bound variable

class ClosureExample {

private int mRes;

Thread makeThreadClosure(String s, int n) {

return new Thread(() ->

System.out.println(s + (mRes += n)));

}

ClosureExample() throw InterruptedException {

Thread t = makeThreadClosure("result = ", 10);

t.start(); t.join();

}

}

Implementing Closures with Lambda Expressions

Constructor creates a closure & runs it in a background thread

Page 43: Overview of Java 8 Lambda Expressions & Method …schmidt/cs891f/2018-PDFs/02...•Java 8’s forEach() method can be used to print out values of an array, e.g. •In conjunction with

43

End of Overview of Java 8 Lambda Expressions

& Method References