Java common function interface - Function Interface

 

JDK provides a number of interface functions common to enrich Lambda typical usage scenario, they are mainly  supplied java.util.function package. Function The following is a simple example of the use of interfaces.

Function Interface Overview

java.util.function.Function <T, R> interface is used to obtain a data type to another data type according to the former is called pre-conditions, which is referred to as post-conditions.

@FunctionalInterface
public interface Function<T, R> {
    R apply(T t);
    ......
}

 

Abstract method: apply

Function main interface is an abstract method: R Apply (T T), get the result type of T R in accordance with the parameter type. Use scenario for example: the conversion of type String type Integer.

import java.util.function.Function;

public class DemoFunctionApply {

    public static void main(String[] args) {
        method(s -> Integer.parseInt(s));
    }
    
    private static void method(Function<String, Integer> function) {
        int num = function.apply("10");
        System.out.println(num + 20);
    }
}

Run the program, the console output:

30

Of course, the best method is by reference to the wording.

 

The default method: andThen

Function interface has a default andThen method used for combining operation. JDK source code such as:

default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
    Objects.requireNonNull(after);
    return (T t) -> after.apply(apply(t));
}

The scenario method is also used for "what to do first, what to do," the, and the Consumer in andThen similar:

import java.util.function.Function;

public class DemoFunctionAndThen {

    public static void main(String[] args) {
        method(
                str -> Integer.parseInt(str)+10,
                i -> i *= 10
        );
    }

    private static void method(Function<String, Integer> one, Function<Integer, Integer> two) {
        int num = one.andThen(two).apply("10");
        System.out.println(num + 20);
    }
}

Run the program, the console output:

220

The first operation is to parse the string become int number, multiplied by 10 is the second operation. Operated by two combined in order to together longitudinal andThen.

Please note, Function preconditions and postconditions generics Generics are the same.

 

Exercise: Custom Function Model splicing

topic

Use Function splicing function model, according to the need to perform a plurality of functions for operating sequence:

String str = "Zhao Liying, 20";
  1. The age of the digital string interception portions, to obtain the string;
  2. The last step is converted into a string of digital type int;
  3. The figures for the step 100 int, int result obtained number.

 

answer

import java.util.function.Function;

public class DemoFunction {

    public static void main(String[] args) {
        String str = "赵丽颖,20";

        int age = getAgeNum(
                str,
                s -> s.split(",")[1],
                s -> Integer.parseInt(s),
                n -> n += 100
        );
        System.out.println(age);
    }

    private static int getAgeNum(String str,
                                 Function<String, String> one,
                                 Function<String, Integer> two,
                                 Function<Integer, Integer> three) {
        return one.andThen(two).andThen(three).apply(str);
    }
}

Run the program, the console output:

120

 

          

Guess you like

Origin www.cnblogs.com/liyihua/p/12286100.html