StaticPL/JAVA

Functional

데먕 2019. 8. 27. 20:46

1. Overview

The term Java functional interface was introduced in Java 8. A functional interface in Java is an interface that contains only a single abstract (unimplemented) method. A functional interface can contain default and static methods which do have an implementation, in addition to the single unimplemented method.

1.1 Without Implementation

public interface MyFunctionalInterface {
    public void execute();
}

1.2 With Implementation

public interface MyFunctionalInterface2{
    public void execute();

    public default void print(String text) {
        System.out.println(text);
    }

    public static void print(String text, PrintWriter writer) throws IOException {
        writer.write(text);
    }
}

2. Relationship with Lambda

A Java functional interface can be implemented by a Java Lambda Expression. A Java lambda expression implements a single method from a Java interface. In order to know what method the lambda expression implements, the interface can only contain a single unimplemented method. In other words, the interface must be a Java functional interface.

MyFunctionalInterface lambda = () -> {
    System.out.println("Executing...");
}

3. Built-in Functional Interfaces

3.1 Function (java.util.function.Function)

The Java Function interface (java.util.function.Function) interface is one of the most central functional interfaces in Java. The Function interface represents a function (method) that takes a single parameter and returns a single value. Here is how the Function interface definition looks:

public interface Function<T,R> {

    public <R> apply(T parameter);
}

The only method you have to implement to implement the Function interface is the apply() method. Here is a Function implementation example:

3.1.1 Declaration

public class AddThree implements Function<Long, Long> {

    @Override
    public Long apply(Long aLong) {
        return aLong + 3;
    }
}

3.1.2 Usage

Function<Long, Long> adder = new AddThree();
Long result = adder.apply((long) 4);
System.out.println("result = " + result);

3.1.3 Lambda case

Function<Long, Long> adder = (value) -> value + 3;
Long resultLambda = adder.apply((long) 8);
System.out.println("resultLambda = " + resultLambda);

3.2 Predicate (java.util.function.Predicate)

The Java Predicate interface represents a simple function that takes a single value as a parameter, and returns true or false.

public interface Predicate {
    boolean test(T t);
}
public class CheckForNull implements Predicate {
    @Override
    public boolean test(Object o) {
        return o != null;
    }
}
Predicate predicate = (value) -> value != null;

3.3 UnaryOperator

The Java UnaryOperator interface is a functional interface that represents an operation that takes a single parameter and returns a parameter of the same type.

UnaryOperator<Person> unaryOperator = 
        (person) -> { person.name = "New Name"; return person; };

3.4 BinaryOperator

The Java BinaryOperator interface is a functional interface that represents an operation that takes two parameters and returns a single value. Both parameters and the return type must be of the same type.

BinaryOperator<MyValue> binaryOperator =
        (value1, value2) -> { value1.add(value2); return value1; };

3.5 Supplier

The Java Supplier interface is a functional interface that represents a function that supplies a value of some sort. The Supplier interface can also be thought of as a factory interface. Here is an example implementation of the Java Supplier interface:

Supplier<Integer> supplier = () -> new Integer((int) (Math.random() * 1000D));

3.6 Consumer

The Java Consumer interface is a functional interface that represents a function that consumes a value without returning any value. A Java Consumer implementation could be printing out the value or writing it to a file, or over the network, etc. Here is an example implementation of the Java Consumer interface:

// This Java Consumer implementation prints the value 
// passed as parameter to it out to System.out.
Consumer<Integer> consumer = (value) -> System.out.println(value);

4. Reference

http://tutorials.jenkov.com/java-functional-programming/functional-interfaces.html