21
Java 8 Functional Interfaces Introduction Douglas C. Schmidt

Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method

Java 8 Functional Interfaces

Introduction

Douglas C. Schmidt

Page 2: Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method

Learning Objectives in This Lesson• Recognize foundational functional programming features in Java 8, e.g.,

• Lambda expressions

• Method & constructor references

• Key functional interfaces

Page 3: Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method

These features are the basis for Java streams & concurrency/parallelism frameworks.

Learning Objectives in This Lesson• Recognize foundational functional programming features in Java 8, e.g.,

• Lambda expressions

• Method & constructor references

• Key functional interfaces

Page 4: Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method

Learning Objectives in This Lesson• Recognize foundational functional programming features in Java 8.

• Understand how to apply these Java 8 features in concise example programs.

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

Page 5: Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method

Learning Objectives in This Lesson• Recognize foundational functional programming features in Java 8.

• Understand how to apply these Java 8 features in concise example programs.

• The examples showcase the Java collections framework.

See docs.oracle.com/javase/8/docs/technotes/guides/collections

Page 6: Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method

Overview of Common Functional Interfaces

Douglas C. Schmidt

Page 7: Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method

• A functional interface contains only one abstract method.

Overview of Common Functional Interfaces

See www.oreilly.com/learning/java-8-functional-interfaces

Page 8: Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method

See www.baeldung.com/java-8-functional-interfaces

• A functional interface is the type used for a parameter when a lambda expression or method reference is passed as an argument to a method.

Overview of Common Functional Interfaces

<T> void runTest(Function<T, T> fact, T n) {

long startTime = System.nanoTime();

T result = fact.apply(n));

long stopTime = (System.nanoTime() - startTime) / 1_000_000;

...

}

runTest(ParallelStreamFactorial::factorial, n);

runTest(SequentialStreamFactorial::factorial, n);

...

Page 9: Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method

See github.com/douglascraigschmidt/LiveLessons/tree/master/Java8/ex16

• A functional interface is the type used for a parameter when a lambda expression or method reference is passed as an argument to a method.

Overview of Common Functional Interfaces

<T> void runTest(Function<T, T> fact, T n) {

long startTime = System.nanoTime();

T result = fact.apply(n));

long stopTime = (System.nanoTime() - startTime) / 1_000_000;

...

}

runTest(ParallelStreamFactorial::factorial, n);

runTest(SequentialStreamFactorial::factorial, n);

...

Records & prints the time taken to compute ‘n’ factorial.

Page 10: Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method

• A functional interface is the type used for a parameter when a lambda expression or method reference is passed as an argument to a method.

Overview of Common Functional Interfaces

<T> void runTest(Function<T, T> fact, T n) {

long startTime = System.nanoTime();

T result = fact.apply(n));

long stopTime = (System.nanoTime() - startTime) / 1_000_000;

...

}

runTest(ParallelStreamFactorial::factorial, n);

runTest(SequentialStreamFactorial::factorial, n);

...

‘fact’ parameterizes the factorial implementation.

See docs.oracle.com/javase/8/docs/api/java/util/function/Function.html

Page 11: Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method

• A functional interface is the type used for a parameter when a lambda expression or method reference is passed as an argument to a method.

Overview of Common Functional Interfaces

<T> void runTest(Function<T, T> fact, T n) {

long startTime = System.nanoTime();

T result = fact.apply(n));

long stopTime = (System.nanoTime() - startTime) / 1_000_000;

...

}

runTest(ParallelStreamFactorial::factorial, n);

runTest(SequentialStreamFactorial::factorial, n);

...

Different factorial implementations can be passed as parameters to runTest().

Page 12: Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method

• A functional interface is the type used for a parameter when a lambda expression or method reference is passed as an argument to a method.

Overview of Common Functional Interfaces

<T> void runTest(Function<T, T> fact, T n) {

long startTime = System.nanoTime();

T result = fact.apply(n));

long stopTime = (System.nanoTime() - startTime) / 1_000_000;

...

}

runTest(ParallelStreamFactorial::factorial, n);

...static BigInteger factorial(BigInteger n) {

return LongStream.rangeClosed(1, n)

.parallel()

.mapToObj(BigInteger::valueOf)

.reduce(BigInteger.ONE, BigInteger::multiply);

}

Page 13: Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method

See docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html

• Java 8 defines many types of functional interfaces.

Overview of Common Functional Interfaces

Page 14: Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method

See www.oreilly.com/library/view/java-8-pocket/9781491901083/ch04.html

• Java 8 defines many types of functional interfaces.

• Some of these interfaceshandle reference types.

Overview of Common Functional Interfaces

Page 15: Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method

• Java 8 defines many types of functional interfaces.

• Some of these interfaceshandle reference types.

• Other interfaces supportprimitive types.

Overview of Common Functional Interfaces

See docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Page 16: Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method

• Java 8 defines many types of functional interfaces.

• Some of these interfaceshandle reference types.

• Other interfaces supportprimitive types.

• Avoids “auto-boxing” overhead.

Overview of Common Functional Interfaces

See rules.sonarsource.com/java/tag/performance/RSPEC-4276

Page 17: Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method

• Java 8 defines many types of functional interfaces.

• Some of these interfaceshandle reference types.

• Other interfaces supportprimitive types.

• There’s an explosion ofJava functional interfaces!

Overview of Common Functional Interfaces

See dzone.com/articles/whats-wrong-java-8-part-ii

Page 18: Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method

• Java 8 defines many types of functional interfaces.

• Some of these interfaceshandle reference types.

• Other interfaces supportprimitive types.

• There’s an explosion ofJava functional interfaces!

• However, learn theseinterfaces before tryingto customize your own.

Overview of Common Functional Interfaces

See tutorials.jenkov.com/java-functional-programming/functional-interfaces.html

Page 19: Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method

Overview of Common Functional Interfaces

FunctionalInterfaces

Predicate

Function

ConsumerSupplier

BiFunction

• Java 8 defines many types of functional interfaces.

• Some of these interfaceshandle reference types.

• Other interfaces supportprimitive types.

• There’s an explosion ofJava functional interfaces!

We focus on the most common types of functional interfaces.

Page 20: Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method

Overview of Common Functional Interfaces

FunctionalInterfaces

Predicate

Function

ConsumerSupplier

BiFunction

• Java 8 defines many types of functional interfaces.

• Some of these interfaceshandle reference types.

• Other interfaces supportprimitive types.

• There’s an explosion ofJava functional interfaces!

All usages of functional interfaces in the upcoming examples are “stateless”!

Page 21: Java 8 Functional Interfacesschmidt/cs891f/2019-PDFs/java... · 2019. 9. 2. · •Recognize foundational functional programming features in Java 8, e.g., •Lambda expressions •Method