Survey of Java 8 Function, Consumer, Supplier, Predicate

Motivation

In Java8, there are lambda, Stream, additional classes, etc., but I feel that Function, Consumer, Supplier, Predicate, etc. are the basis, so I understand it properly.

Function

How to use

A mechanism to handle the behavior of methods in Java with an instance. The method definition that was conventionally done as follows,

MethodDefinition


R functionName(T arg) {
    return new R();
}

It can be changed to an instance definition in the following form.

ConventionalDefinition


Function<T, R> functionalInstance = new Function<T, R>() {
    @Override
    public R apply(T arg) {
        return new R();
    }
};

This is a hassle, so you can use a lambda expression to do the following:

InstanceDefinition


Function<T, R> functionalInstance = arg -> new R();

In the case of method definition, it was called as follows,

MethodCall


R returnValue = functionName((T) argument);

If you use Function, use the apply method,

InstanceCall


R returnValue = functionalInstance.apply((T) argument);

And.

Methods in Java are not first-class citizens, but it seems like we've created a new mechanism to make it easier to introduce such styles.

Consumer Once you know the Function, it's easy, and the Consumer is an interface for defining instances that consume arguments. Since the argument is taken but the return value does not exist, there is only one type specification by generics on the argument side.

As an image, an instance version of the following method.

void consumer(T arg) {
    something(arg);
}

The definition method is as follows. Override the method called accept to determine the behavior.

Consumer<T> consumer = new Consumer<T>() {
    @Override
    public void accept(T arg) {
        something(arg);
    }

For lambda, below.

Consumer<T> consumer = arg -> something(arg);

The call is made as follows.

consumer.accept((T) argument)

Supplier Supplier is an interface for defining behavior that takes no arguments but has a return value.

As an image, it is for making an instance version of the following method.

T consumer() {
    return new T();
}

It is defined as follows.

Supplier<T> supplier = new Supplier<T>() {
    @Override
    public T get() {
        return new T();
    }
}

If it is lambda, it is as follows.

Supplier<T> supplier = () -> new T();

When using it, do as follows.

supplier.get();

Predicate Predicate is an interface that receives an argument and returns a boolean value.

//True if the argument is 1, false otherwise
Predicate<Integer> predicate = arg -> arg == 1;

Integer argument = 1;
if (predicate.test(argument)) {
    System.out.println("argument is 1");
} else {
    System.out.println("argument is not 1");
}

I use it like that.

I think it's close to Function <T, Boolean>, but in this case there is unboxing from Boolean to boolean, so is it one purpose to avoid it? There seems to be a method called and or or, but I will omit it because it seems to be rarely used.

If you want a complete understanding of synchronized

Explanation of synchronized and typical error example I wrote, so please check it out as well. Experience shows that about 70% of people are wrong.

Recommended Posts

Survey of Java 8 Function, Consumer, Supplier, Predicate
Implementation of like function in Java
[JQuery] Implementation procedure of AutoComplete function [Java / Spring]
[Java] Overview of Java
A survey of the Kubernetes native Java framework Quarkus
Expired collection of java
Predicted Features of Java
[Java] Significance of serialVersionUID
NIO.2 review of java
Review of java Shilber
java --Unification of comments
History of Java annotation
[Note] Java: String survey
java (merits of polymorphism)
Implementation of pagination function
NIO review of java
Java 8 Completable Future Survey
[Java] Three features of Java
Summary of Java support 2018
Comparison of how to write Callback function (Java, JavaScript, Ruby)