📜  Java 8 | Java中的BiConsumer接口与示例

📅  最后修改于: 2022-05-13 01:54:39.505000             🧑  作者: Mango

Java 8 | Java中的BiConsumer接口与示例

BiConsumer 接口Java .util 的一部分。从Java 8 开始引入的函数包,用于在Java中实现函数式编程。它表示一个接受两个参数并产生结果的函数。但是,这些类型的函数不返回任何值。

此功能接口采用两个泛型,即:-

  • T :表示操作的第一个输入参数的类型
  • U :表示操作的第二个输入参数的类型

分配给 BiConsumer 类型对象的 lambda 表达式用于定义其accept()最终将给定操作应用于其参数。

BiConsumers 在不需要返回任何值时很有用,因为它们预计会通过副作用进行操作。

BiConsumer 接口中的函数

BiConsumer 接口由以下两个函数组成:

1. 接受()

此方法接受两个值并对给定的参数执行操作

句法:

void accept(T t, U u)

参数:该方法有两个参数:

  • t - 第一个输入参数
  • u - 第二个输入参数

返回:此方法不返回任何值。

下面是说明 accept() 方法的代码:

程序 1:使用 BiConsumer 的 accept() 方法比较 2 个整数列表的程序:

Java
// Java Program to demonstrate
// BiConsumer's accept() method
 
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
 
public class GFG {
    public static void main(String args[])
    {
 
        // Create the first list
        List lista = new ArrayList();
        lista.add(2);
        lista.add(1);
        lista.add(3);
 
        // Create the second list
        List listb = new ArrayList();
        listb.add(2);
        listb.add(1);
        listb.add(2);
 
        // BiConsumer to compare both lists
        BiConsumer, List >
            equals = (list1, list2) ->
        {
            if (list1.size() != list2.size()) {
                System.out.println("False");
            }
            else {
                for (int i = 0; i < list1.size(); i++)
                    if (list1.get(i) != list2.get(i)) {
                        System.out.println("False");
                        return;
                    }
                System.out.println("True");
            }
        };
        equals.accept(lista, listb);
    }
}


Java
// Java Program to demonstrate
// BiConsumer's andThen() method
 
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
 
public class Main {
    public static void main(String args[])
    {
 
        // Create first list
        List lista = new ArrayList();
        lista.add(2);
        lista.add(1);
        lista.add(3);
 
        // Create second list
        List listb = new ArrayList();
        listb.add(2);
        listb.add(1);
        listb.add(2);
 
        // BiConsumer to compare 2 lists of integers
        BiConsumer, List > equals = (list1, list2) ->
        {
            if (list1.size() != list2.size()) {
                System.out.println("False");
            }
            else {
                for (int i = 0; i < list1.size(); i++)
                    if (list1.get(i) != list2.get(i)) {
                        System.out.println("False");
                        return;
                    }
                System.out.println("True");
            }
        };
 
        // BiConsumer to print 2 lists
        BiConsumer, List > disp = (list1, list2) ->
        {
            list1.stream().forEach(a -> System.out.print(a + " "));
            System.out.println();
            list2.stream().forEach(a -> System.out.print(a + " "));
            System.out.println();
        };
 
        // Using addThen() method
        equals.andThen(disp).accept(lista, listb);
    }
}


Java
// Java Program to demonstrate
// BiConsumer's andThen() method
 
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
 
public class Main {
    public static void main(String args[])
    {
 
        // Create first list
        List lista = new ArrayList();
        lista.add(2);
        lista.add(1);
        lista.add(3);
 
        // Create second list
        List listb = new ArrayList();
        listb.add(2);
        listb.add(1);
        listb.add(2);
 
        // BiConsumer to compare 2 lists of integers
        BiConsumer, List > equals = (list1, list2) ->
        {
            if (list1.size() != list2.size()) {
                System.out.println("False");
            }
            else {
                for (int i = 0; i < list1.size(); i++)
                    if (list1.get(i) != list2.get(i)) {
                        System.out.println("False");
                        return;
                    }
                System.out.println("True");
            }
        };
 
        // BiConsumer to print 2 lists
        BiConsumer, List > disp = (list1, list2) ->
        {
            list1.stream().forEach(a -> System.out.print(a + " "));
            System.out.println();
            list2.stream().forEach(a -> System.out.print(a + " "));
            System.out.println();
        };
 
        try {
            equals.andThen(null).accept(lista, listb);
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}


Java
// Java Program to demonstrate
// BiConsumer's andThen() method
 
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
 
public class Main {
    public static void main(String args[])
    {
 
        // Create first list
        List lista = new ArrayList();
        lista.add(2);
        lista.add(1);
        lista.add(3);
 
        // Create second list
        List listb = new ArrayList();
        listb.add(2);
        listb.add(1);
 
        // BiConsumer to compare 2 lists of integers
        BiConsumer, List > equals = (list1, list2) ->
        {
            for (int i = 0; i < list1.size(); i++)
                if (list1.get(i) != list2.get(i)) {
                    System.out.println("False");
                    return;
                }
            System.out.println("True");
        };
 
        // BiConsumer to print 2 lists
        BiConsumer, List > disp = (list1, list2) ->
        {
            list1.stream().forEach(a -> System.out.print(a + " "));
            System.out.println();
            list2.stream().forEach(a -> System.out.print(a + " "));
            System.out.println();
        };
 
        try {
            disp.andThen(equals).accept(lista, listb);
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}


输出:
False

2.然后()

它返回一个组合的 BiConsumer,其中参数化的 BiConsumer 将在第一个之后执行。如果任一操作的评估引发错误,则会将其转发给组合操作的调用者。

注意:作为参数传递的操作应该是 BiConsumer 类型。

句法:

default BiConsumer  
        andThen(BiConsumer after)

参数:此方法接受一个参数,在该参数之后是要在当前参数之后应用的 BiConsumer。
返回值:此方法返回一个组合的 BiConsumer,它首先应用当前操作,然后是 after 操作。

异常:如果 after 操作为 null,则此方法抛出NullPointerException

下面是说明 andThen() 方法的代码:

方案一:

Java

// Java Program to demonstrate
// BiConsumer's andThen() method
 
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
 
public class Main {
    public static void main(String args[])
    {
 
        // Create first list
        List lista = new ArrayList();
        lista.add(2);
        lista.add(1);
        lista.add(3);
 
        // Create second list
        List listb = new ArrayList();
        listb.add(2);
        listb.add(1);
        listb.add(2);
 
        // BiConsumer to compare 2 lists of integers
        BiConsumer, List > equals = (list1, list2) ->
        {
            if (list1.size() != list2.size()) {
                System.out.println("False");
            }
            else {
                for (int i = 0; i < list1.size(); i++)
                    if (list1.get(i) != list2.get(i)) {
                        System.out.println("False");
                        return;
                    }
                System.out.println("True");
            }
        };
 
        // BiConsumer to print 2 lists
        BiConsumer, List > disp = (list1, list2) ->
        {
            list1.stream().forEach(a -> System.out.print(a + " "));
            System.out.println();
            list2.stream().forEach(a -> System.out.print(a + " "));
            System.out.println();
        };
 
        // Using addThen() method
        equals.andThen(disp).accept(lista, listb);
    }
}
输出:
False
2 1 3 
2 1 2

程序2:演示何时返回NullPointerException

Java

// Java Program to demonstrate
// BiConsumer's andThen() method
 
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
 
public class Main {
    public static void main(String args[])
    {
 
        // Create first list
        List lista = new ArrayList();
        lista.add(2);
        lista.add(1);
        lista.add(3);
 
        // Create second list
        List listb = new ArrayList();
        listb.add(2);
        listb.add(1);
        listb.add(2);
 
        // BiConsumer to compare 2 lists of integers
        BiConsumer, List > equals = (list1, list2) ->
        {
            if (list1.size() != list2.size()) {
                System.out.println("False");
            }
            else {
                for (int i = 0; i < list1.size(); i++)
                    if (list1.get(i) != list2.get(i)) {
                        System.out.println("False");
                        return;
                    }
                System.out.println("True");
            }
        };
 
        // BiConsumer to print 2 lists
        BiConsumer, List > disp = (list1, list2) ->
        {
            list1.stream().forEach(a -> System.out.print(a + " "));
            System.out.println();
            list2.stream().forEach(a -> System.out.print(a + " "));
            System.out.println();
        };
 
        try {
            equals.andThen(null).accept(lista, listb);
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}
输出
Exception : java.lang.NullPointerException

程序3:演示函数返回和处理后的异常。

Java

// Java Program to demonstrate
// BiConsumer's andThen() method
 
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
 
public class Main {
    public static void main(String args[])
    {
 
        // Create first list
        List lista = new ArrayList();
        lista.add(2);
        lista.add(1);
        lista.add(3);
 
        // Create second list
        List listb = new ArrayList();
        listb.add(2);
        listb.add(1);
 
        // BiConsumer to compare 2 lists of integers
        BiConsumer, List > equals = (list1, list2) ->
        {
            for (int i = 0; i < list1.size(); i++)
                if (list1.get(i) != list2.get(i)) {
                    System.out.println("False");
                    return;
                }
            System.out.println("True");
        };
 
        // BiConsumer to print 2 lists
        BiConsumer, List > disp = (list1, list2) ->
        {
            list1.stream().forEach(a -> System.out.print(a + " "));
            System.out.println();
            list2.stream().forEach(a -> System.out.print(a + " "));
            System.out.println();
        };
 
        try {
            disp.andThen(equals).accept(lista, listb);
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}
输出
2 1 3 
2 1 
Exception : java.lang.IndexOutOfBoundsException: Index 2 out of bounds for length 2