Java8 常用函数式接口

本文主要参考:http://blog.csdn.net/jiangchao858/article/details/73730038 在此感谢

消费型接口Consumer< T >

接收一个参数T,没有返回值
源码:

@FunctionalInterface
public interface Consumer<T> {
    void accept(T t);
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}

示例

//讲述一个学生在学习过程中,需要买学习用品。买什么东西是具体的实现,我们在调用study()方法的时候再指定。
    @Test
    public void testConsumer(){
        study(6, (m -> System.out.println("买工具书花费" + m + "元")));
        study(2, (m -> System.out.println("买铅笔花费" + m + "元")));
    }

    public void study(double money, Consumer<Double> con){
        con.accept(money);
    }

函数型接口Function< T, R >

传入参数T做一定的操作,返回R型的数据
源码:

@FunctionalInterface
public interface Function<T, R> {
    R apply(T t);
    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }
    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }
    static <T> Function<T, T> identity() {
        return t -> t;
    }
}

示例:

//这里的studentUpdate()接受一个student参数,做一定的处理之后,返回一个string类型的值。
//例如更改一个学生的名字,打印更改以后的名字。当然最好可以返回学生对象,这样就可以更改年龄、得分之类的信息。
    /**
     * 对学生改名字,将Aim改为Anny
     */
    @Test
    public void testFunction(){
        String newName = studentUpdate(students.get(0), (s) -> {s.setName("Anny"); return s.getName();});
        System.out.println(newName);
    }

    /**
     * 
     * @param 学生对象
     * @param 要做什么处理,在调用该方法的时候指定
     * @return
     */
    public String studentUpdate(Student s, Function<Student,String> fun){
        return fun.apply(s);
    }

供给型接口Supplier< T >

不接受参数,返回特定类型参数T
源码:

@FunctionalInterface
public interface Supplier<T> {
    T get();
}

示例

//模拟老师随机点名,这次需要三位同学打扫卫生,使用Supplier来生成对应的学生信息。
    public static List<Student> students = Arrays.asList(
            new Student(1, "Aim", 12, 78),
            new Student(2, "Bob", 15, 45),
            new Student(3, "Cicy", 13, 89),
            new Student(4, "Deny", 11, 91),
            new Student(5, "Emiy", 14, 32)
    );
    @Test
    public void testSupplier(){
        List<Student> stuList = getStuList(3, () -> students.get((int)(Math.random()*4) + 1));
        System.out.println(stuList);
    }
    /**
     * 
     * @param num 需要多少学生信息
     * @param sup 怎么去筛选需要的学生信息
     * @return list 学生集合
     */
    public List<Student> getStuList(int num, Supplier<Student> sup){
        List<Student> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            Student s = sup.get();
            list.add(s);
        }
        return list;
    }

断言型接口Predicate< T >

输入一个参数,返回boolean类型
源码:

@FunctionalInterface
public interface Predicate<T> {
    boolean test(T t);
    default Predicate<T> and(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) && other.test(t);
    }
    default Predicate<T> negate() {
        return (t) -> !test(t);
    }
    default Predicate<T> or(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) || other.test(t);
    }
    static <T> Predicate<T> isEqual(Object targetRef) {
        return (null == targetRef)
                ? Objects::isNull
                : object -> targetRef.equals(object);
    }
}

示例

    /**
     * 把学生成绩大于60的学生信息打印输出
     */
    @Test
    public void testPredicate(){
        List<Student> filterStudent = filterStudent(students, (s) -> s.getScore() >= 60);
        filterStudent.stream().forEach(System.out::println);
    }

    /**
     * 
     * @param 学生集合list
     * @param 需要做何种判断
     * @return 符合条件的学生集合list
     */
    public List<Student> filterStudent(List<Student> list, Predicate<Student> pre){
        List<Student>  l = new ArrayList<>();

        for(Student s : list){
            if (pre.test(s)) {
                l.add(s);
            }
        }
        return l;
}

其他函数式接口

接口 参数类型 返回类型 用途
BiFunction< T, U, R > T, U R 对类型T,U参数操作,返回R类型的结果
UnaryOperator< T > T T 对类型为T的对象进行一元运算,返回T类型的结果
BinaryOperator< T> T, T T 对类型为T的对象进行二元运算,返回T类型的结果
BiConsumer< T, U> T, U void 对类型为T,U参数应用操作
ToIntFunction< T> T int 计算int值的计算
IntFunction< R> int R 参数为int类型的函数

猜你喜欢

转载自blog.csdn.net/u012562117/article/details/79207626