Lecture 11 Objectives
At the end of today's class you should
KNOW:
- That to abstract over behavior in Java (pre-Java 8), one must design a class that implements a method,
then use an instance of the class (a function object) as an argument to another method
- That Java 8 provides built-in functional interfaces to facilitate
method abstraction
- That Java 8 provides lambda expressions to enable the passing of
functionality as a method argument
BE ABLE TO:
- Define an interface to represent a predicate
- Use the interface to define a method that can be passed as a function
object to another method
- Use Java's built-in Predicate<T> interface
- Use Java's built-in Function<T,R> interface
- Write a lambda expression
- Write test cases for methods that have function objects as parameters
Sample Exam Question:
Here is a method:
// produce a list of the dillos in the given list, brought back to life
LinkedList<Dillo> reviveAllDillos(LinkedList<Dillo> dilloList){
LinkedList<Dillo> aliveDillos = new LinkedList<Dillo>();
for (Dillo d: dilloList){
aliveDillos.add(new Dillo(d.length, true));
}
return aliveDillos;
}
Create a Function object that transforms a dillo from its current state
(either dead or alive) to alive. Then rewrite the given function using a
Function parameter. Finally, call your rewritten method with your Function
object.