Functional Interface
CS2030S
In CS2030s, we have implemented a bunch of functional interfaces. I feel they are very important for the future topics, so I document them down here.
BooleanCondition<T>::test
package cs2030s.fp;
/**
* Represent a conditional statement that returns either true of false.
* CS2030S
* AY23/24 Semester 2
*
* @param <T> The type of the variable to be tested with this conditional statement.
*/
@FunctionalInterface
public interface BooleanCondition<T> {
/**
* The functional method to test if the condition is true/false on the given value t.
*
* @param t The variable to test
* @return The return value of the test.
*/
boolean test(T t);
}Explanation
Parameter Type is T and return type is boolean.
Lambda Example
This means whenever the method parameter is a BooleanCondition<T>, we can pass a lambda expression which is similar to the following as the method argument.
BooleanCondition<Integer> isPositive = x -> x > 0;Producer<T>::produce
package cs2030s.fp;
/**
* Represent a function that produce a value.
* CS2030S
* AY23/24 Semester 2
*
* @param <T> The type of the value produced.
*/
@FunctionalInterface
public interface Producer<T> {
/**
* The functional method to produce a value.
*
* @return The value produced.
*/
T produce();
}Explanation
No parameter and return type is T.
Lambda Example
This means whenever the method parameter is a Producer<T>, we can pass a lambda expression which is similar to the following as the method argument.
Producer<Double> randomValue = () -> Math.random();Consumer<T>::consume
package cs2030s.fp;
/**
* Represent a function that consumes a value.
* CS2030S
* AY23/24 Semester 2
*
* @param <T> The type of the value consumed.
*/
@FunctionalInterface
public interface Consumer<T> {
/**
* The functional method to consume a value.
*
* @param t The value consumed.
*/
void consume(T t);
}Explanation
Parameter type is T return type is void
Lambda example
This means whenever the method parameter is a Consumer<T>, we can pass a lambda expression which is similar to the following as the method argument.
Consumer<String> printUpperCase = s -> System.out.println(s.toUpperCase());Transformer<U, T>::transform
package cs2030s.fp;
/**
* Represent a function that transforms one value into another, possible of different types.
* CS2030S
* AY23/24 Semester 2
*
* @param <U> The type of the input value
* @param <T> The type of the result value
*/
@FunctionalInterface
public interface Transformer<U, T> {
/**
* The function method to transform the value u.
*
* @param u The input value
* @return The value after applying the given transformation on u.
*/
T transform(U u);
}Explanation
Parameter type is U, return type is T. Can think it as transforming one value of type U into another value of type T.
Lambda Example
This means whenever the method parameter is a Transformer<T>, we can pass a lambda expression which is similar to the following as the method argument.
Transfomer<String, Integer> stringLength = s -> s.length();Combiner<S, T, R>::combine
package cs2030s.fp;
/**
* Represent a function that combines two values into one. The two inputs
* and the result can be of different types.
* CS2030S
* AY23/24 Semester 2
*
* @param <S> The type of the first input value
* @param <T> The type of the second input value
* @param <R> The type of the return value
*/
@FunctionalInterface
public interface Combiner<S, T, R> {
/**
* The function method to combines two values into one.
*
* @param s The first input value
* @param t The second input value
* @return The value after combining s and t.
*/
R combine(S s, T t);
}Explanation
Two parameters of type S, T respectively, return type is R.
Lambda Example
This means whenever the method parameter is a Combiner<T>, we can pass a lambda expression which is similar to the following as the method argument.
Combiner<Integer, Integer, Integer> multiply = (a, b) -> a * b;Runnable
@FunctionalInterface
public interface Runnable {
/**
* When an object implementing interface Runnable is used to create a thread,
* starting the thread causes the object's run method to be called in that
* separately executing thread.
*/
void run();
}Lambda Example
This means whenever the method parameter is a Runnable, we can pass a lambda expression which is similar to the following as the method argument.
Runnable task = () -> {
System.out.println("Running in thread: " + Thread.currentThread().getName());
};Java
Java provides its own version of functional interfaces that are comparable to ours, in the java.util.function package. The table below shows some commonly used ones:
BooleanCondition<T>::test
Producer<T>::produce
Consumer<T>::consume
Transformer<T, R>::transform
Transformer<T, T>::transform
Combiner<S, T, R>::combine
Last updated