Skip to content
章节导航

四大核心函数

Consumer:消费型接口

对类型为 T 的对象应用操作,没有返回值,方法为 void accept(T t)

案例 1

java
public static void main(String[] args) {
    toConsumer(200, (x) -> System.out.println("accept:"+x));
}

public static void toConsumer(double a, Consumer<Double> b) {
    b.accept(a);
}

控制台输出:

shell
accept:200.0

案例 2

shell
public static void main(String[] args) {
    /* 基于自定义实现 */
    Consumer<String> con = new Consumer<String>() {
        @Override
        public void accept(String str) {
            System.out.println("自定义 Consumer,使用 Stream 遍历信息: " + str);
        }
    };
    Stream<String> s = Stream.of("333", "444", "555", "666", "777");
    s.forEach(con);
    System.out.println("=============================================");

    /* 基于 Lambda 表达式 */
    s = Stream.of("333", "444", "555", "666", "777");
    Consumer<String> c = (I) -> System.out.println("基于 Lambda 表达式:"+I);
    s.forEach(c);
    System.out.println("=============================================");

    /* 通过方法引用的方式,代替了 Lambda 表达式  */
    s = Stream.of("333", "444", "555", "666", "777");
    Consumer c3 = System.out::println;
    s.forEach(c3);
}

控制台输出

shell
自定义 Consumer,使用 Stream 遍历信息: 333
自定义 Consumer,使用 Stream 遍历信息: 444
自定义 Consumer,使用 Stream 遍历信息: 555
自定义 Consumer,使用 Stream 遍历信息: 666
自定义 Consumer,使用 Stream 遍历信息: 777
=============================================
基于 Lambda 表达式:333
基于 Lambda 表达式:444
基于 Lambda 表达式:555
基于 Lambda 表达式:666
基于 Lambda 表达式:777
=============================================
333
444
555
666
777

Supplier:供给型接口

产生类型为 T 的对象

案例 1

循环 10 次,生成 30 以内的随机数

shell
public static void main(String[] args) {
    Random r = new Random();
    List<Integer> list = randomNumbers( () -> r.nextInt(30));
    for (Integer n : list) {
        System.out.println(n);
    }
}

public static List<Integer> randomNumbers(Supplier<Integer> s1) {
    List<Integer> l = new ArrayList<Integer>();
    for (int j = 0; j < 10; j++) {
        l.add(s1.get());
    }
    return l;
}

控制台输出:

shell
14
25
9
1
5
27
28
12
13
24

案例 2

shell
public static void main(String[] args) {
    /* 基于类 */
    Supplier<Integer> su = new Supplier<Integer>() {
        @Override
        public Integer get() {
            return new Random().nextInt();
        }
    };
    System.out.println(su.get());
    System.out.println("=============================================");

    /* 基于 Lambda 表达式 */
    su = () -> new Random().nextInt();
    System.out.println(su.get());
    System.out.println("=============================================");

    /* 通过方法引用的方式,代替了 Lambda 表达式 */
    Supplier<Double> su2 = Math::random;
    System.out.println(su2.get());
}

控制台输出

shell
339152702
=============================================
-595605879
=============================================
0.7313288546192285

Function:函数式接口

对类型为 T 的对象应用操作,并返回结果,结果是 R 类型的对象,包含方法:

R apply(T t);

案例

shell
public static void main(String[] args) {
        String s = st("abc", n -> n.substring(0, 1));
        System.out.println(s);

        String s1 = st(" ab c ", n -> n.trim());
        System.out.println(s1);
    }

    public static String st(String s, Function<String, String> fun) {
        return fun.apply(s);
    }

控制台输出

shell
a
ab c

案例 2

shell
public static void main(String[] args) {
    Function<String, Integer> f = new Function<String, Integer>() {
        @Override
        public Integer apply(String i) {
            return i.length();
        }
    };

    Stream<String> s = Stream.of("555", "887", "990");
    Stream<Integer> s1 = s.map(f);
    s1.forEach(System.out::println);
}

Predicate:断言型接口

确定类型 T 的对象是否满足某种约束,并返回 boolean 值,包含方法: boolean test(T t);

案例 1

shell
public static void main(String[] args) {
        List<Integer> ll = new ArrayList<>();
        ll.add(10);
        ll.add(192);
        ll.add(131);
        ll.add(821);
        ll.add(18);

        /* 筛选大于 100 的数 */
        List<Integer> l = fInt(ll, n -> (n > 100));
        for (Integer i : l) {
            System.out.println(i);
        }
    }

    public static List<Integer> fInt(List<Integer> li, Predicate<Integer> p) {
        List<Integer> l = new ArrayList<>();
        for (Integer integer : li) {
            if (p.test(integer))
                l.add(integer);
        }
        return l;
    }

控制台输出

shell
192
131
821

案例 2

shell
public static void main(String[] args) {
    /* 基于类 */
    Predicate<Integer> p = new Predicate<Integer>() {
        @Override
        public boolean test(Integer integer) {
            if (integer > 7) {
                return true;
            }
            return false;
        }
    };
    System.out.println(p.test(9));
    System.out.println("=============================================");

    /* 基于 Lambda 表达式 */
    p = (t) -> t > 8;
    System.out.println(p.test(5));
}

控制台输出

shell
true
=============================================
false