72
Overview of Java 8 Functional Interfaces Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

  • Upload
    others

  • View
    17

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

Overview of Java 8 Functional Interfaces

Douglas C. [email protected]

www.dre.vanderbilt.edu/~schmidt

Professor of Computer ScienceInstitute for Software Integrated Systems

Vanderbilt University Nashville, Tennessee, USA

Page 2: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

2

Learning Objectives in this Lesson• Recognize foundational functional

programming features in Java 8, e.g.,• Lambda expressions • Method & constructor references• Functional interfaces

Page 3: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

3These features are the foundation for Java 8’s concurrency/parallelism frameworks

Learning Objectives in this Lesson• Recognize foundational functional

programming features in Java 8, e.g.,• Lambda expressions • Method & constructor references• Functional interfaces

Page 4: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

4

Learning Objectives in this Lesson• Recognize foundational functional

programming features in Java 8• Understand how these Java 8 features

are applied in concise example programs

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

Page 5: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

5

Overview of Common Functional Interfaces

Page 6: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

6

• A functional interface contains only one abstract methodOverview of Common Functional Interfaces

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

Page 7: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

7See 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

Overview of Common Functional Interfaces

<T> void runTest(Function<T, T> fact, T n) {System.out.println(n + " factorial = " + fact.apply(n));

}

runTest(ParallelStreamFactorial::factorial, n);

...

Page 8: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

8

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

Overview of Common Functional Interfaces

static BigInteger factorial(BigInteger n) { return LongStream.rangeClosed(1, n).parallel() .mapToObj(BigInteger::valueOf).reduce(BigInteger.ONE, BigInteger::multiply);

}

<T> void runTest(Function<T, T> fact, T n) {System.out.println(n + " factorial = " + fact.apply(n));

}

runTest(ParallelStreamFactorial::factorial, n);

...

Page 9: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

9See 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 10: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

10

• Java 8 defines many types of functional interfaces• This list is large due to the

need to support reference types & primitive types..

Overview of Common Functional Interfaces

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

Page 11: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

11

Overview of Common Functional Interfaces

FunctionalInterfaces

Predicate

Function

ConsumerSupplier

BiFunction

• Java 8 defines many types of functional interfaces• This list is large due to the

need to support reference types & primitive types..

We focus on the most common types of functional interfaces

Page 12: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

12

Overview of Common Functional Interfaces

FunctionalInterfaces

Predicate

Function

ConsumerSupplier

BiFunction

• Java 8 defines many types of functional interfaces• This list is large due to the

need to support reference types & primitive types..

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

Page 13: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

13

Overview of Functional Interfaces:Predicate, Function, &

BiFunction

Page 14: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

14

• A Predicate performs a test that returns true or false, e.g.,• public interface Predicate<T> { boolean test(T t); }

Overview of Common Functional Interfaces: Predicate

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

Page 15: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

15

• A Predicate performs a test that returns true or false, e.g.,• public interface Predicate<T> { boolean test(T t); }

Overview of Common Functional Interfaces: Predicate

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

Page 16: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

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

• A Predicate performs a test that returns true or false, e.g.,• public interface Predicate<T> { boolean test(T t); }

Overview of Common Functional Interfaces: Predicate

Map<String, Integer> iqMap = new ConcurrentHashMap<String, Integer>() { { put("Larry", 100); put("Curly", 90); put("Moe", 110);

} };

System.out.println(iqMap);

iqMap.entrySet().removeIf(entry -> entry.getValue() <= 100);

System.out.println(iqMap);

Page 17: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

17

Map<String, Integer> iqMap = new ConcurrentHashMap<String, Integer>() { { put("Larry", 100); put("Curly", 90); put("Moe", 110);

} };

System.out.println(iqMap);

iqMap.entrySet().removeIf(entry -> entry.getValue() <= 100);

System.out.println(iqMap);

• A Predicate performs a test that returns true or false, e.g.,• public interface Predicate<T> { boolean test(T t); }

Overview of Common Functional Interfaces: Predicate

This predicate lambda deletes entries with iq <= 100

Page 18: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

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

• A Predicate performs a test that returns true or false, e.g.,• public interface Predicate<T> { boolean test(T t); }

Overview of Common Functional Interfaces: Predicate

Map<String, Integer> iqMap = new ConcurrentHashMap<String, Integer>() { { put("Larry", 100); put("Curly", 90); put("Moe", 110);

} };

System.out.println(iqMap);

iqMap.entrySet().removeIf(entry -> entry.getValue() <= 100);

System.out.println(iqMap);

entry is short for (EntrySet entry), which leverages the type inference

capabilities of Java 8’s compiler

Page 19: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

19

• A Predicate performs a test that returns true or false, e.g.,• public interface Predicate<T> { boolean test(T t); }

Overview of Common Functional Interfaces: Predicate

interface Collection<T> { ...default boolean removeIf(Predicate<? super E> filter) {...final Iterator<E> each = iterator();while (each.hasNext()) {if (filter.test(each.next())) {each.remove();

...

Here’s how the removeIf() method uses the Predicate passed to it

Page 20: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

20

• A Predicate performs a test that returns true or false, e.g.,• public interface Predicate<T> { boolean test(T t); }

Overview of Common Functional Interfaces: Predicate

interface Collection<T> { ...default boolean removeIf(Predicate<? super E> filter) {...final Iterator<E> each = iterator();while (each.hasNext()) {if (filter.test(each.next())) {each.remove();

...

entry -> entry.getValue() <= 100

This predicate lambda returns true if an element is <= too the value 100

Page 21: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

21

interface Collection<T> { ...default boolean removeIf(Predicate<? super E> filter) {...final Iterator<E> each = iterator();while (each.hasNext()) {if (filter.test(each.next())) {each.remove();

...

entry -> entry.getValue() <= 100

• A Predicate performs a test that returns true or false, e.g.,• public interface Predicate<T> { boolean test(T t); }

Overview of Common Functional Interfaces: Predicate

The ‘entry’ in the lambda predicate is replaced by the parameter to test()

if (each.next().getValue() <= 100)

Page 22: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

22

• A Function applies a computation on 1 parameter & returns a result, e.g.,• public interface Function<T, R> { R apply(T t); }

Overview of Common Functional Interfaces: Function

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

Page 23: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

23

• A Function applies a computation on 1 parameter & returns a result, e.g.,• public interface Function<T, R> { R apply(T t); }

Overview of Common Functional Interfaces: Function

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

Page 24: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

24

• A Function applies a computation on 1 parameter & returns a result, e.g.,• public interface Function<T, R> { R apply(T t); }

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

Overview of Common Functional Interfaces: Function

Map<Integer, Integer> primeCache = new ConcurrentHashMap<>();

...Long smallestFactor = primeCache.computeIfAbsent

(primeCandidate, (key) -> primeChecker(key));...

Integer primeChecker(Integer primeCandidate) {... // Determines if a number if prime

}

Page 25: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

25

• A Function applies a computation on 1 parameter & returns a result, e.g.,• public interface Function<T, R> { R apply(T t); }

Overview of Common Functional Interfaces: Function

Map<Integer, Integer> primeCache = new ConcurrentHashMap<>();

...Long smallestFactor = primeCache.computeIfAbsent

(primeCandidate, (key) -> primeChecker(key));...

Integer primeChecker(Integer primeCandidate) {... // Determines if a number if prime

}

This method provides atomic “check then act” semantics

See dig.cs.illinois.edu/papers/checkThenAct.pdf

Page 26: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

26

• A Function applies a computation on 1 parameter & returns a result, e.g.,• public interface Function<T, R> { R apply(T t); }

Overview of Common Functional Interfaces: Function

Map<Integer, Integer> primeCache = new ConcurrentHashMap<>();

...Long smallestFactor = primeCache.computeIfAbsent

(primeCandidate, (key) -> primeChecker(key));...

Integer primeChecker(Integer primeCandidate) {... // Determines if a number if prime

}

A lambda expression that calls a function

Page 27: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

27

• A Function applies a computation on 1 parameter & returns a result, e.g.,• public interface Function<T, R> { R apply(T t); }

Overview of Common Functional Interfaces: Function

Map<Integer, Integer> primeCache = new ConcurrentHashMap<>();

...Long smallestFactor = primeCache.computeIfAbsent

(primeCandidate, this::primeChecker);...

Integer primeChecker(Integer primeCandidate) {... // Determines if a number if prime

}

Could also be a passed as a method reference

Page 28: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

28

• A Function applies a computation on 1 parameter & returns a result, e.g.,• public interface Function<T, R> { R apply(T t); }

Overview of Common Functional Interfaces: Function

class ConcurrentHashMap<K,V> ...public V computeIfAbsent(K key,

Function<? super K, ? extends V> mappingFunction) {...if ((f = tabAt(tab, i = (n - 1) & h)) == null)...if ((val = mappingFunction.apply(key)) != null)node = new Node<K,V>(h, key, val, null);

...

Here’s how the computeIfAbsent() method uses the function passed to it

Page 29: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

29

• A Function applies a computation on 1 parameter & returns a result, e.g.,• public interface Function<T, R> { R apply(T t); }

Overview of Common Functional Interfaces: Function

class ConcurrentHashMap<K,V> ...public V computeIfAbsent(K key,

Function<? super K, ? extends V> mappingFunction) {...if ((f = tabAt(tab, i = (n - 1) & h)) == null)...if ((val = mappingFunction.apply(key)) != null)node = new Node<K,V>(h, key, val, null);

...

this::primeChecker

The lambda function is bound to this::primeChecker method reference

Page 30: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

30

• A Function applies a computation on 1 parameter & returns a result, e.g.,• public interface Function<T, R> { R apply(T t); }

Overview of Common Functional Interfaces: Function

class ConcurrentHashMap<K,V> ...public V computeIfAbsent(K key,

Function<? super K, ? extends V> mappingFunction) {...if ((f = tabAt(tab, i = (n - 1) & h)) == null)...if ((val = mappingFunction.apply(key)) != null)node = new Node<K,V>(h, key, val, null);

...

The apply() method is replaced with the primeChecker() lambda function

if ((val = primeChecker(key)) != null)

Page 31: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

31

• A BiFunction applies a computation on 2 parameters & returns a result, e.g.,• public interface BiFunction<T, U, R> { R apply(T t, U u); }

Overview of Common Functional Interfaces: BiFunction

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

Page 32: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

32

• A BiFunction applies a computation on 2 parameters & returns a result, e.g.,• public interface BiFunction<T, U, R> { R apply(T t, U u); }

Overview of Common Functional Interfaces: BiFunction

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

Page 33: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

33

• A BiFunction applies a computation on 2 parameters & returns a result, e.g.,• public interface BiFunction<T, U, R> { R apply(T t, U u); }

Overview of Common Functional Interfaces: BiFunction

Map<String, Integer> iqMap = new ConcurrentHashMap<String, Integer>() {{ put("Larry", 100); put("Curly", 90); put("Moe", 110); }

};

for (Map.Entry<String, Integer> entry : iqMap.entrySet())entry.setValue(entry.getValue() - 50);

vs.

iqMap.replaceAll((k, v) -> v - 50);

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

Page 34: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

34

• A BiFunction applies a computation on 2 parameters & returns a result, e.g.,• public interface BiFunction<T, U, R> { R apply(T t, U u); }

Overview of Common Functional Interfaces: BiFunction

Map<String, Integer> iqMap = new ConcurrentHashMap<String, Integer>() {{ put("Larry", 100); put("Curly", 90); put("Moe", 110); }

};

for (Map.Entry<String, Integer> entry : iqMap.entrySet())entry.setValue(entry.getValue() - 50);

vs.

iqMap.replaceAll((k, v) -> v - 50);

Conventional way of subtracting 50 IQ points from each person in map

Page 35: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

35

• A BiFunction applies a computation on 2 parameters & returns a result, e.g.,• public interface BiFunction<T, U, R> { R apply(T t, U u); }

Overview of Common Functional Interfaces: BiFunction

Map<String, Integer> iqMap = new ConcurrentHashMap<String, Integer>() {{ put("Larry", 100); put("Curly", 90); put("Moe", 110); }

};

for (Map.Entry<String, Integer> entry : iqMap.entrySet())entry.setValue(entry.getValue() - 50);

vs.

iqMap.replaceAll((k, v) -> v - 50);

BiFunctional lambda subtracts 50 IQ points from each person in map

Unlike Entry operations, however, replaceAll() operates in a thread-safe manner!

Page 36: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

36

• A BiFunction applies a computation on 2 parameters & returns a result, e.g.,• public interface BiFunction<T, U, R> { R apply(T t, U u); }

Overview of Common Functional Interfaces: BiFunction

class ConcurrentHashMap<K,V> {...public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {...for (Node<K,V> p; (p = it.advance()) != null; ) {

V oldValue = p.val;for (K key = p.key;;) {V newValue = function.apply(key, oldValue);...replaceNode(key, newValue, oldValue)...

Here’s how the replaceAll() method uses the BiFunction passed to it

Page 37: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

37

• A BiFunction applies a computation on 2 parameters & returns a result, e.g.,• public interface BiFunction<T, U, R> { R apply(T t, U u); }

Overview of Common Functional Interfaces: BiFunction

class ConcurrentHashMap<K,V> {...public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {...for (Node<K,V> p; (p = it.advance()) != null; ) {

V oldValue = p.val;for (K key = p.key;;) {V newValue = function.apply(key, oldValue);...replaceNode(key, newValue, oldValue)...

(k, v) -> v - 50

The bifunction parameter is bound to the lambda expression v - 50

Page 38: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

38

• A BiFunction applies a computation on 2 parameters & returns a result, e.g.,• public interface BiFunction<T, U, R> { R apply(T t, U u); }

Overview of Common Functional Interfaces: BiFunction

class ConcurrentHashMap<K,V> {...public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {...for (Node<K,V> p; (p = it.advance()) != null; ) {

V oldValue = p.val;for (K key = p.key;;) {V newValue = function.apply(key, oldValue);...replaceNode(key, newValue, oldValue)...

The apply() method is replaced by the v – 50 bifunction lambda

V newValue = oldValue - 50

(k, v) -> v - 50

Page 39: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

39

Overview of Functional Interfaces: Consumer & Supplier

Page 40: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

40

• A Consumer accepts a parameter & returns no results, e.g.,• public interface Consumer<T> { void accept(T t); }

Overview of Common Functional Interfaces: Consumer

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

Page 41: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

41

• A Consumer accepts a parameter & returns no results, e.g.,• public interface Consumer<T> { void accept(T t); }

Overview of Common Functional Interfaces: Consumer

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

Page 42: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

42

List<Thread> threads =Arrays.asList(new Thread("Larry"),

new Thread("Curly"),new Thread("Moe"));

threads.forEach(System.out::println);threads.sort(Comparator.comparing(Thread::getName));threads.forEach(System.out::println);

• A Consumer accepts a parameter & returns no results, e.g.,• public interface Consumer<T> { void accept(T t); }

Overview of Common Functional Interfaces: Consumer

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

Page 43: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

43

List<Thread> threads =Arrays.asList(new Thread("Larry"),

new Thread("Curly"),new Thread("Moe"));

threads.forEach(System.out::println);threads.sort(Comparator.comparing(Thread::getName));threads.forEach(System.out::println);

• A Consumer accepts a parameter & returns no results, e.g.,• public interface Consumer<T> { void accept(T t); }

Overview of Common Functional Interfaces: Consumer

See docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html#forEach-java.util.function.Consumer-

Print out threads using forEach()

Page 44: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

44

public interface Iterable<T> {...default void forEach(Consumer<? super T> action) {for (T t : this) {action.accept(t);

}}

• A Consumer accepts a parameter & returns no results, e.g.,• public interface Consumer<T> { void accept(T t); }

Overview of Common Functional Interfaces: Consumer

Here’s how the forEach() method uses the Consumer passed to it

Page 45: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

45

public interface Iterable<T> {...default void forEach(Consumer<? super T> action) {for (T t : this) {action.accept(t);

}}

• A Consumer accepts a parameter & returns no results, e.g.,• public interface Consumer<T> { void accept(T t); }

Overview of Common Functional Interfaces: Consumer

System.out::println

The consumer lambda parameter is bound to println() on the System.out field

Page 46: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

46

public interface Iterable<T> {...default void forEach(Consumer<? super T> action) {for (T t : this) {action.accept(t);

}}

• A Consumer accepts a parameter & returns no results, e.g.,• public interface Consumer<T> { void accept(T t); }

Overview of Common Functional Interfaces: Consumer

The accept() method is replaced by the call to System.out.println()

System.out.println(t)

Page 47: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

47

• This example also shows a Function, e.g.,• public interface Function<T, R> { R apply(T t); }

List<Thread> threads =new ArrayList<>(Arrays.asList(new Thread("Larry"),

new Thread("Curly"),new Thread("Moe")));

threads.forEach(System.out::println);threads.sort(Comparator.comparing(Thread::getName));threads.forEach(System.out::println);

Overview of Common Functional Interfaces: Consumer

Page 48: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

48

• This example also shows a Function, e.g.,• public interface Function<T, R> { R apply(T t); }

List<Thread> threads =new ArrayList<>(Arrays.asList(new Thread("Larry"),

new Thread("Curly"),new Thread("Moe")));

threads.forEach(System.out::println);threads.sort(Comparator.comparing(Thread::getName));threads.forEach(System.out::println);

A method reference used to sort threads by name

Overview of Common Functional Interfaces: Consumer

Page 49: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

49

• This example also shows a Function, e.g.,• public interface Function<T, R> { R apply(T t); }

A method reference used to sort threads by name

List<Thread> threads =new ArrayList<>(Arrays.asList(new Thread("Larry"),

new Thread("Curly"),new Thread("Moe")));

threads.forEach(System.out::println);threads.sort(Comparator.comparing(Thread::getName));threads.forEach(System.out::println);

Overview of Common Functional Interfaces: Consumer

See docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#comparing-java.util.function.Function-

Page 50: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

50

• This example also shows a Function, e.g.,• public interface Function<T, R> { R apply(T t); }

interface Comparator {... static <T, U extends Comparable<? super U>> Comparator<T>

comparing(Function<? super T, ? extends U> keyEx) {return ((c1, c2) ->

keyEx.apply(c1).compareTo(keyEx.apply(c2)); }

Overview of Common Functional Interfaces: Consumer

Here’s how the comparing() method uses the Function passed to it

Page 51: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

51

• This example also shows a Function, e.g.,• public interface Function<T, R> { R apply(T t); }

Overview of Common Functional Interfaces: Consumer

The Thread::getName() method reference is bound to the keyEx parameter

interface Comparator {... static <T, U extends Comparable<? super U>> Comparator<T>

comparing(Function<? super T, ? extends U> keyEx) {return ((c1, c2) ->

keyEx.apply(c1).compareTo(keyEx.apply(c2)); }

Thread::getName()

Page 52: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

52

• This example also shows a Function, e.g.,• public interface Function<T, R> { R apply(T t); }

Overview of Common Functional Interfaces: Consumer

The getName() methods are then called to compare the two thread names

c1.getName().compareTo(c2.getName())

interface Comparator {... static <T, U extends Comparable<? super U>> Comparator<T>

comparing(Function<? super T, ? extends U> keyEx) {return ((c1, c2) ->

keyEx.apply(c1).compareTo(keyEx.apply(c2)); }

Page 53: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

53

• A Supplier returns a value & takes no parameters, e.g.,• public interface Supplier<T> { T get(); }

Overview of Common Functional Interfaces: Supplier

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

Page 54: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

54

• A Supplier returns a value & takes no parameters, e.g.,• public interface Supplier<T> { T get(); }

Overview of Common Functional Interfaces: Supplier

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

Page 55: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

55

• A Supplier returns a value & takes no parameters, e.g.,• public interface Supplier<T> { T get(); }

Overview of Common Functional Interfaces: Supplier

Map<String, String> beingMap = new HashMap<String, String>() { { put("Demon", "Naughty"); put("Angel", "Nice"); } };

String being = ...;

Optional<String> disposition = Optional.ofNullable(beingMap.get(being));

System.out.println("disposition of "+ being + " = "+ disposition.orElseGet(() -> "unknown"));

Returns default value if being not found

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

Page 56: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

56

• A Supplier returns a value & takes no parameters, e.g.,• public interface Supplier<T> { T get(); }

Overview of Common Functional Interfaces: Supplier

Map<String, String> beingMap = new HashMap<String, String>() { { put("Demon", "Naughty"); put("Angel", "Nice"); } };

String being = ...;

Optional<String> disposition = Optional.ofNullable(beingMap.get(being));

System.out.println("disposition of "+ being + " = "+ disposition.orElseGet(() -> "unknown"));

Returns default value if being is not found

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

Page 57: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

57

• A Supplier returns a value & takes no parameters, e.g.,• public interface Supplier<T> { T get(); }

Overview of Common Functional Interfaces: Supplier

class Optional<T> {...public T orElseGet(Supplier<? extends T> other) {return value != null ? value : other.get();

}

Here’s how the orElseGet() method uses the Supplier passed to it

Page 58: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

58The string literal "unknown" is bound to the supplier lambda parameter

• A Supplier returns a value & takes no parameters, e.g.,• public interface Supplier<T> { T get(); }

Overview of Common Functional Interfaces: Supplier

class Optional<T> {...public T orElseGet(Supplier<? extends T> other) {return value != null ? value : other.get();

}

() -> "unknown"

Page 59: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

59

• A Supplier returns a value & takes no parameters, e.g.,• public interface Supplier<T> { T get(); }

Overview of Common Functional Interfaces: Supplier

class Optional<T> {...public T orElseGet(Supplier<? extends T> other) {return value != null ? value : other.get();

}

: "unknown"

() -> "unknown"

The string "unknown" returns by orElseGet() if the value is null

Page 60: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

60

• A constructor reference is also a Supplier, e.g.,• public interface Supplier<T> { T get(); }

Overview of Common Functional Interfaces: Supplier

class CrDemo {public static void main(String[] argv) {

Supplier<CrDemo> supplier = CrDemo::new;System.out.println(supplier.get().hello());

}

private String hello() {return "hello";

}}

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

Page 61: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

61

• A constructor reference is also a Supplier, e.g.,• public interface Supplier<T> { T get(); }

Overview of Common Functional Interfaces: Supplier

class CrDemo {public static void main(String[] argv) {

Supplier<CrDemo> supplier = CrDemo::new;System.out.println(supplier.get().hello());

}

private String hello() {return "hello";

}}

See www.speakingcs.com/2014/08/constructor-references-in-java-8.html

Create a supplier object that’s initialized with a constructor reference for class CrDemo

Page 62: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

62

Overview of Common Functional Interfaces: Supplier• A constructor reference is also a Supplier, e.g.,

• public interface Supplier<T> { T get(); }

class CrDemo {public static void main(String[] argv) {

Supplier<CrDemo> supplier = CrDemo::new;System.out.println(supplier.get().hello());

}

private String hello() {return "hello";

}}

get() creates a CrDemo object using the constructor reference

Page 63: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

63

Overview of Common Functional Interfaces: Supplier• A constructor reference is also a Supplier, e.g.,

• public interface Supplier<T> { T get(); }

class CrDemo {public static void main(String[] argv) {

Supplier<CrDemo> supplier = CrDemo::new;System.out.println(supplier.get().hello());

}

private String hello() {return "hello";

}}

Calls a method in CrDemo

Page 64: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

64

Other Properties of Functional Interfaces

Page 65: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

65

• A functional interface may also have default methods or static methods

interface Comparator {int compare(T o1, T o2);

boolean equals(Object obj);

default Comparator<T> reversed() { return Collections.reverseOrder(this); }

static <T extends Comparable<? super T>> Comparator<T> reverseOrder() { return Collections.reverseOrder(); }...

Other Properties of Functional Interfaces

See docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

Page 66: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

66

• A functional interface may also have default methods or static methods, e.g.

interface Comparator {int compare(T o1, T o2);

boolean equals(Object obj);

default Comparator<T> reversed() { return Collections.reverseOrder(this); }

static <T extends Comparable<? super T>> Comparator<T> reverseOrder() { return Collections.reverseOrder(); }...

Other Properties of Functional Interfaces

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

Page 67: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

67

• A functional interface may also have default methods or static methods, e.g.,

interface Comparator {int compare(T o1, T o2);

boolean equals(Object obj);

default Comparator<T> reversed() { return Collections.reverseOrder(this); }

static <T extends Comparable<? super T>> Comparator<T> reverseOrder() { return Collections.reverseOrder(); }...

Other Properties of Functional Interfaces

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

Page 68: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

68

• A functional interface may also have default methods or static methods, e.g.,

interface Comparator {int compare(T o1, T o2);

boolean equals(Object obj);

default Comparator<T> reversed() { return Collections.reverseOrder(this); }

static <T extends Comparable<? super T>> Comparator<T> reverseOrder() { return Collections.reverseOrder(); }...

Other Properties of Functional Interfaces

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

Page 69: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

69

• A functional interface may also have default methods or static methods, e.g.,

interface Comparator {int compare(T o1, T o2);

boolean equals(Object obj);

default Comparator<T> reversed() { return Collections.reverseOrder(this); }

static <T extends Comparable<? super T>> Comparator<T> reverseOrder() { return Collections.reverseOrder(); }...

Other Properties of Functional Interfaces

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

Page 70: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

70

• A functional interface may also have default methods or static methods, e.g.,

interface Comparator {int compare(T o1, T o2);

boolean equals(Object obj);

default Comparator<T> reversed() { return Collections.reverseOrder(this); }

static <T extends Comparable<? super T>> Comparator<T> reverseOrder() { return Collections.reverseOrder(); }...

Other Properties of Functional Interfaces

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

Page 71: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

71

• A functional interface may also have default methods or static methods, e.g.,

interface Comparator {int compare(T o1, T o2);

boolean equals(Object obj);

default Comparator<T> reversed() { return Collections.reverseOrder(this); }

static <T extends Comparable<? super T>> Comparator<T> reverseOrder() { return Collections.reverseOrder(); }...

See docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html

An abstract method that overrides a public java.lang.Object method

does not count as part of the interface's abstract method count

Other Properties of Functional Interfaces

Page 72: Overview of Java 8 Functional Interfacesschmidt/cs891f/2017-PDFs/... · 2. Learning Objectives in this Lesson • Recognize foundational functional programming features in Java 8,

72

End of Overview of Java 8 Functional Interfaces