Java8 新特性之函数式接口

函数式接口,就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口,let's coding。

(1)Consumer

package java.util.function;

import java.util.Objects;

/**
 * Represents an operation that accepts a single input argument and returns no
 * result. Unlike most other functional interfaces, {@code Consumer} is expected
 * to operate via side-effects.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #accept(Object)}.
 *
 * @param <T> the type of the input to the operation
 *
 * @since 1.8
 */
@FunctionalInterface
public interface Consumer<T> {

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);

    /**
     * Returns a composed {@code Consumer} that performs, in sequence, this
     * operation followed by the {@code after} operation. If performing either
     * operation throws an exception, it is relayed to the caller of the
     * composed operation.  If performing this operation throws an exception,
     * the {@code after} operation will not be performed.
     *
     * @param after the operation to perform after this operation
     * @return a composed {@code Consumer} that performs in sequence this
     * operation followed by the {@code after} operation
     * @throws NullPointerException if {@code after} is null
     */
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}

该类使用@FunctionInterface注解表明该类是函数式接口

实例:

package function;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

/**
 *
 * @author smart 2019/3/25
 */
public class ConsumerInterface {
    public static void main(String[] args) {
        Consumer<Integer> consumer = (Integer t)-> System.out.println(t);
        Consumer<Integer> consumer1 = (Integer t)-> System.out.println(t+1);
        List<Integer> result = Arrays.asList(1,2,3);
        result.stream().forEach(consumer.andThen(consumer1));
    }
}

(2)同理可以学习 Supplier,Predict,Functional 接口,下面主要给出实例

(2.1)Supplier实例

package function;

/**
 *
 * @author smart 2019/3/25
 */
public class Student {
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }

    private String name;
}
package function;

import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;

/**
 *
 * @author smart 2019/3/25
 */
public class SupplierInterface {
    public static void main(String[] args) {
        Supplier<Student> supplier = ()-> {
            Student student = new Student();
            student.setName("12222");
            return student;
        };
        Student student = supplier.get();
        System.out.println(student);
    }
}

(2.2)Predict

package function;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 *
 * @author smart 2019/3/25
 */
public class PredictInterface {

    public static void main(String[] args) {
        List<Integer> result = Arrays.asList(1,2,3);
        Predicate<Integer> predicate = (t)->{
            if(t%2==0){
                return true;
            }
            return false;
        };
        System.out.println(result.stream().anyMatch(predicate));
        System.out.println(result.stream().allMatch(predicate));

    }
}

(2.3)Function

package function;

import java.util.function.Function;

/**
 *
 * @author smart 2019/3/25
 */
public class FunctionInterface {
    public static void main(String[] args) {
        Function<Integer,String> function = (t)->
        {return String.valueOf(t);};
        function.apply(new Integer(2000));
    }
}

猜你喜欢

转载自blog.csdn.net/u014046563/article/details/88809267