Consumer

消费型接口,用来消费 有参数,无返回值 accept

接口原型


@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); };
    }
}

示例代码

package optional;

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


public class JDKInnerInterface {

    public static <T> void sayHello(T name, Consumer<T> con) {
        con.accept(name);
    }

    public static <T> void forEach(List<T> list, Consumer<T> con) {
        // 依次遍历消费
        for (T t : list) {
            con.accept(t);
        }
    }


    public static void main(String[] args) {

        // lambda方式
        sayHello("jack", name -> System.out.println("Hello," + name));
        // 方法引入
        sayHello("tom", System.out::println);


        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        // 模拟 forEach遍历
        forEach(list, System.out::println);
    }
}

Supplier

供给型接口,用来供给 没有参数,只有返回值 get

接口原型

@FunctionalInterface
public interface Supplier<T> {

    /**
     * Gets a result.
     *
     * @return a result
     */
    T get();
}

示例代码

    // Supplier 生产者
    public static class InnerSup {
        // 随机生成一个 List
        public static <E> List<E> genRanList(Supplier<E> sup, int size) {
            ArrayList<E> es = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                es.add(sup.get());
            }
            return es;
        }

        public static void main(String[] args) {
            Random random = new Random();
            // lambda
            genRanList(() -> random.nextInt(10), 10).forEach(System.out::println);
            // 方法引入
            genRanList(random::nextBoolean, 5).forEach(System.out::println);
        }
    }

Function

函数型接口,有输入,有返回值 apply

接口原型


@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;
    }
}

示例代码

    // function 函数型接口 一个输入一个输出
    public static class InnerFunc {
        public static String strOper(String str, Function<String, String> fun) {
            return fun.apply(str);
        }

        public static void main(String[] args) {
            System.out.println(strOper("Hello,", s -> s + "java!"));
            System.out.println(strOper("HELLO,WORLD!", String::toLowerCase));
        }
    }

Predicate

断言,输入参数,返回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);
    }
}

示例代码

    // Predicate 断言
    public static class InnerPre {
        public static <E> List<E> filter(List<E> list, Predicate<E> predicate) {
            ArrayList<E> es = new ArrayList<>();
            for (E e : list) {
                Optional.ofNullable(e).filter(s -> !predicate.test(s)).ifPresent(es::add);
            }
            return es;
        }

        public static void main(String[] args) {
            List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6);
            // 过滤偶数
            filter(integers, s -> s % 2 == 0).forEach(System.out::println);

            System.out.println("============");

            // 过滤大于2的数
            filter(integers, s -> s > 2).forEach(System.out::println);
        }
    }

完整代码

package optional;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;


/**
 * @author luckyfang
 * @date 2021.9.12
 * @desc  人类高质量代码-函数式编程之谁用if谁是🐶系列
 */

public class JDKInnerInterface {
    // Consumer 消费者
    public static class InnerCon {
        public static <T> void sayHello(T name, Consumer<T> con) {
            con.accept(name);
        }
        public static <T> void forEach(List<T> list, Consumer<T> con) {
            // 依次遍历消费
            for (T t : list) {
                con.accept(t);
            }
        }
        public static void main(String[] args) {

            // lambda方式
            sayHello("jack", name -> System.out.println("Hello," + name));
            // 方法引入
            sayHello("tom", System.out::println);


            ArrayList<Integer> list = new ArrayList<>();
            list.add(1);
            list.add(2);
            list.add(3);
            // 模拟 forEach遍历
            forEach(list, System.out::println);
        }
    }


    // Supplier 生产者
    public static class InnerSup {
        // 随机生成一个 List
        public static <E> List<E> genRanList(Supplier<E> sup, int size) {
            ArrayList<E> es = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                es.add(sup.get());
            }
            return es;
        }

        public static void main(String[] args) {
            Random random = new Random();
            // lambda
            genRanList(() -> random.nextInt(10), 10).forEach(System.out::println);
            // 方法引入
            genRanList(random::nextBoolean, 5).forEach(System.out::println);
        }
    }


    // function 函数型接口 一个输入一个输出
    public static class InnerFunc {
        public static String strOper(String str, Function<String, String> fun) {
            return fun.apply(str);
        }

        public static void main(String[] args) {
            System.out.println(strOper("Hello,", s -> s + "java!"));
            System.out.println(strOper("HELLO,WORLD!", String::toLowerCase));
        }
    }
    
    // Predicate 断言
    public static class InnerPre {
        public static <E> List<E> filter(List<E> list, Predicate<E> predicate) {
            ArrayList<E> es = new ArrayList<>();
            for (E e : list) {
                Optional.ofNullable(e).filter(s -> !predicate.test(s)).ifPresent(es::add);
            }
            return es;
        }

        public static void main(String[] args) {
            List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6);
            // 过滤偶数
            filter(integers, s -> s % 2 == 0).forEach(System.out::println);

            System.out.println("============");

            // 过滤大于2的数
            filter(integers, s -> s > 2).forEach(System.out::println);
        }
    }

}

Q.E.D.


全栈混子的菜鸡日常