【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
    评论
Java 8引入了许多新特性,以下是其中一些重要的特性: 1. Lambda表达式:Lambda表达式是Java 8中最重要的特性之一。它允许我们以更简洁的方式编写匿名函数。Lambda表达式可以作为参数传递给方法,或者用于函数式接口的实现。 2. 函数式接口:函数式接口是只包含一个抽象方法的接口。Java 8引入了一些新的函数式接口,如`Predicate`、`Consumer`、`Supplier`和`Function`等。这些接口可以与Lambda表达式一起使用,使得编写函数式代码更加方便。 3. Stream API:Stream API是Java 8中处理集合数据的新方式。它提供了一种流式操作集合的方式,可以进行过滤、映射、排序等操作。Stream API可以大大简化集合数据的处理,并且可以利用多核处理器进行并行操作。 4. 默认方法:默认方法是接口中的一个新特性,它允许在接口中定义具有默认实现的方法。这样一来,当接口的实现类没有实现该方法时,就会使用默认实现。 5. 方法引用:方法引用是一种更简洁地调用已经存在的方法的方式。它可以替代Lambda表达式,使得代码更加简洁易读。 6. 新的日期和时间API:Java 8引入了全新的日期和时间API,取代了旧的`java.util.Date`和`java.util.Calendar`类。新的API提供了更好的日期和时间处理方式,并且支持时区、日历等功能。 7. Optional类:Optional类是一个容器对象,可以包含null或非null的值。它可以避免空指针异常,并且提供了一些便利的方法来处理可能为空的值。 8. 并发增强:Java 8对并发编程进行了一些增强,引入了新的并发工具类,如`CompletableFuture`和`StampedLock`,以及新的并行操作方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值