【Java 8 新特性】JBiConsumer跟BiFunction的区别详解

1、Comsumer和BiConsumer方法

1.1 联系

在这里插入图片描述

首先看一下两个接口,几乎差不多,BiConsumer可以看做Consumer的增强, 区别在于BiConsumer方法的参数多了一个

1.2 方法详解

  • Consumer或BiConsumer的作用就是定义一个函数,然后对其进行消费处理(accept方法);
  • andThen方法相当于组合两个方法,返回一个新的方法,是先对给定的参数进行定义的操作然后在执行after操作
  • BiConsumer就是Consumer的加强版,处理两个参数,默认方法andThen也是一样的

Consumer不返回值,它接受一个参数作为参数
BiConsumer不返回值,它接受两个参数作为参数

1.3 举例

1.3.1 Consumer
package org.example;

import java.util.function.Consumer;

public class Sugar_Consumer{


    public static void main(String[] args) {
        Consumer<Integer> action1 = (x) -> {
            System.out.println("对传进来的进行加1操作: " + (x + 1));
        };

        Consumer<Integer> action2 = (x) -> {
            System.out.println("对传进来的进行减1操作: " + (x - 1));
        };

        Consumer<Integer> anction3 = action1.andThen(action2);
        //先执行加法在执行减法
        System.out.println("执行anction1");
        action1.accept(3);
        System.out.println("执行anction2");
        action2.accept(3);
        System.out.println("执行anction3");
        anction3.accept(3);
    }

}


执行anction1
对传进来的进行加1操作: 4
执行anction2
对传进来的进行减1操作: 2
执行anction3
对传进来的进行加1操作: 4
对传进来的进行减1操作: 2
1.3.2 BiConsumer
package org.example;

import java.util.function.BiConsumer;

public class Sugar_BiConsumer {

    public static void main(String[] args) {
        BiConsumer<Integer, Integer> action1 = (x, y) -> {
            System.out.println("对传进来的进行相加操作: " + (x + y));
        };
        BiConsumer<Integer, Integer> action2 = (x, y) -> {
            System.out.println("对传进来的进行相减操作: " + (x - y));
        };
        BiConsumer<Integer, Integer> anction3 = action1.andThen(action2);

        //先执行加法在执行减法
        System.out.println("执行anction1");
        action1.accept(1, 1);
        System.out.println("执行anction2");
        action2.accept(1, 1);
        System.out.println("执行anction3");
        anction3.accept(1, 1);
    }

}

执行anction1
对传进来的进行相加操作: 2
执行anction2
对传进来的进行相减操作: 0
执行anction3
对传进来的进行相加操作: 2
对传进来的进行相减操作: 0

2、Function和BiFunction

2.1 联系

在这里插入图片描述
先看源码,其实感觉跟Consumer都差不多。

2.2 方法详解

  • Function或BiFunction 的功能方法是apply函数,接收参数为T,返回R类型
  • compose和andThen都是差不多的,都是组合成一个新的函数,然后按照不同顺序执行
  • identity是一个静态方法,返回接收的参数,就是接收啥返回对应的函数

Function有返回值,它接受一个参数作为参数
BiFunction有返回值,它接受两个参数作为参数

2.3 举例

2.3.1 Function
package org.example;

import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Sugar_Function {

    public static void main(String[] args) {
        Function<Integer, Integer> action1 = (x) -> {
            System.out.println("对传进来的进行加1操作: " + (x + 1));
            return x + 1;
        };
        Function<Integer, Integer> action2 = (x) -> {
            System.out.println("对传进来的进行-1操作: " + (x - 1));
            return (x - 1);
        };
        Function<Integer, Integer> action3 = action1.andThen(action2);
        Function<Integer, Integer> compose = action1.compose(action2);

        //先执行加法在执行减法
        System.out.println("执行anction1");
        action1.apply(1);
        System.out.println("执行anction2");
        action2.apply(1);
        System.out.println("执行andThen返回的函数");
        action3.apply(1);
        System.out.println("执行compose返回的函数");
        compose.apply(1);

        // **********************************************************
        // Function.identity()返回一个输出跟输入一样的Lambda表达式对象,等价于形如t -> t形式的Lambda表达式
        // **********************************************************
        Stream<String> stream = Stream.of("I", "love", "you", "too", "greatly");

        // 下面三行的作用是相同的,返回的是原来的字符串.
        
        // Map<String, Integer> map1 = stream.collect(Collectors.toMap(Function.identity(), String::length));
        //System.out.println(map1);

        // Map<String, Integer> map2 = stream.collect(Collectors.toMap(String::toString, String::length));
        // System.out.println(map2);

        Map<String, Integer> map3  = stream.collect(Collectors.toMap(str->str, String::length));
        System.out.println(map3);

    }
}

执行anction1
对传进来的进行加1操作: 2
执行anction2
对传进来的进行-1操作: 0
执行andThen返回的函数
对传进来的进行加1操作: 2
对传进来的进行-1操作: 1
执行compose返回的函数
对传进来的进行-1操作: 0
对传进来的进行加1操作: 1
{love=4, too=3, I=1, greatly=7, you=3}

2.3.2 BiFunction
package org.example;

import java.util.function.BiFunction;
import java.util.function.Function;

public class Sugar_BiFunction {

    public static void main(String[] args) {
        BiFunction<Integer, Integer, Integer> action = (x, y) -> {
            System.out.println(x + y);
            return x + y;
        };
        Function<Integer, Integer> action2 = (x) -> {
            System.out.println("Function..." + x);
            return x;
        };
        BiFunction<Integer, Integer, Integer> integerIntegerBiConsumer = action.andThen(action2);

        integerIntegerBiConsumer.apply(1, 1);
    }
}

2
Function...2

参考:https://blog.csdn.net/qq_31635851/article/details/116933108

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值