[JAVA] Basic functional interface that can be understood in 3 minutes

Introduction

This article is an article that introduces an implementation example of a basic functional interface.

Normally, I would implement it in a lambda expression or use it in a Stream, but for the sake of simplicity, I dare to use a familiar method instead of using it.

I wish I could get the feeling that "methods go into variables". (Strictly speaking, it's a class)

There are 43 functional interfaces defined in the java.util.function package, but the first four are the basics to keep in mind. Other than these four, it is safe to think that they are extended types.

Supplier Someone who has something and gives it to me. The abstract method is get. No arguments, with return value.

import java.time.LocalDateTime;
import java.util.function.Supplier;

public class Main {
    public static void main(String[] args) {
        Supplier<LocalDateTime> s = LocalDateTime::now;
        System.out.println(s.get()); // -> 2017-01-11T16:29:08.086
    }
}

Consumer A person who processes what you receive. The abstract method is accept. With arguments, no return value.

import java.util.function.Consumer;

public class Main {
    public static void main(String[] args) {
        Consumer<String> c = System.out::println;
        c.accept("test"); // -> test
    }
}

Predicate A person who returns the truth of what he received. The abstract method is test. With arguments, with return value (Boolean only).

import java.util.function.Predicate;

public class Main {
    public static void main(String[] args) {
        Predicate<String> p = ""::equals;
        System.out.println(p.test("foo")); // -> false
    }
}

Function A so-called function that processes and returns what you receive. The abstract method is apply. There are arguments and there is a return value.

import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<String, Integer> f = Integer::parseInt;
        System.out.println(f.apply("55") + 45); // -> 100
    }
}

Recommended Posts

Basic functional interface that can be understood in 3 minutes
Introduction to Rakefile that can be done in about 10 minutes
Java (super beginner edition) that can be understood in 180 seconds
Object-oriented that can be understood by fairies
Write a class that can be ordered in Java
Convenient shortcut keys that can be used in Eclipse
List of devices that can be previewed in Swift UI
Create a jar file that can be executed in Gradle
Reference memo / In-memory LDAP server that can be embedded in Java
Summary of ORM "uroboroSQL" that can be used in enterprise Java
Imitate Java's functional interface in Kotlin
SwiftUI View that can be used in combination with other frameworks
Introduction to Java that can be understood even with Krillin (Part 1)
[Android Studio] Description that can be continuously input in SQLite Database [Java]
Log out a CSV file that can be read in Excel using logback
Technology excerpt that can be used for creating EC sites in Java training
I didn't know that inner classes could be defined in the [Java] interface
[Ruby] Basic key to be strong in refactoring
Organize methods that can be used with StringUtils
[Ruby] Methods that can be used with strings
The story that the port can no longer be used in the Spring boot sample program