1、BiConsumer简介
BiConsumer
是 Java 8 引入的一个函数式接口,函数式接口(Functional Interface)的概念被引入,这是为了支持Lambda表达式和方法的引用。函数式接口是仅包含一个抽象方法的接口,这样的接口可以被隐式地转换成函数式接口的实现,从而允许你直接传递代码作为参数,位于java.util.function
包中。BiConsumer
它表示一个接受两个输入参数且不返回任何结果的操作。通常用于需要处理两个参数的场景,例如遍历 Map、批量处理数据等。
2、BiConsumer接口
@FunctionalInterface
public interface BiConsumer<T, U> {void accept(T t, U u);default BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after) {Objects.requireNonNull(after);return (l, r) -> {accept(l, r);after.accept(l, r);};}
}
-
accept(T t, U u)
:接受两个参数并执行操作。 -
andThen(BiConsumer after)
:组合多个BiConsumer
,按顺序执行。
3、基本用法
BiConsumer
通常用于需要处理两个参数的场景。
示例如下:
import java.util.function.BiConsumer;public class BiConsumerExample {public static void main(String[] args) {// 定义一个 BiConsumer,打印两个参数BiConsumer<String, Integer> printDetails = (name, age) -> {System.out.println("Name: " + name + ", Age: " + age);};// 调用 accept 方法printDetails.accept("Alice", 25); // 输出: Name: Alice, Age: 25printDetails.accept("Bob", 30); // 输出: Name: Bob, Age: 30}
}
4、常用场景
4.1、遍历Map
BiConsumer
可以用于遍历 Map
,因为 Map.forEach
方法接受一个 BiConsumer
作为参数。
import java.util.HashMap;
import java.util.Map;public class MapExample {public static void main(String[] args) {Map<String, Integer> map = new HashMap<>();map.put("Alice", 25);map.put("Bob", 30);map.put("Charlie", 35);// 使用 BiConsumer 遍历 Mapmap.forEach((name, age) -> {System.out.println("Name: " + name + ", Age: " + age);});}
}
4.2、批量处理数据
BiConsumer
可以用于批量处理两个相关的数据集。
import java.util.function.BiConsumer;
import java.util.Arrays;
import java.util.List;public class BatchProcessingExample {public static void main(String[] args) {List<String> names = Arrays.asList("Alice", "Bob", "Charlie");List<Integer> ages = Arrays.asList(25, 30, 35);// 定义一个 BiConsumer,打印姓名和年龄BiConsumer<String, Integer> printDetails = (name, age) -> {System.out.println("Name: " + name + ", Age: " + age);};// 批量处理for (int i = 0; i < names.size(); i++) {printDetails.accept(names.get(i), ages.get(i));}}
}
4.3、组合多个BigConsumer
使用 andThen
方法可以将多个 BiConsumer
组合在一起,按顺序执行。
import java.util.function.BiConsumer;public class AndThenExample {public static void main(String[] args) {// 定义第一个 BiConsumerBiConsumer<String, Integer> printDetails = (name, age) -> {System.out.println("Name: " + name + ", Age: " + age);};// 定义第二个 BiConsumerBiConsumer<String, Integer> printUpperCase = (name, age) -> {System.out.println("Name in uppercase: " + name.toUpperCase());};// 组合两个 BiConsumerBiConsumer<String, Integer> combinedConsumer = printDetails.andThen(printUpperCase);// 执行组合后的 BiConsumercombinedConsumer.accept("Alice", 25);}
}
输出结果:
Name: Alice, Age: 25
Name in uppercase: ALICE
4.4、自定义BigConsumer
你可以根据需要自定义 BiConsumer
,实现复杂的逻辑。
import java.util.function.BiConsumer;public class CustomBiConsumerExample {public static void main(String[] args) {// 自定义 BiConsumer,计算两个数的和并打印BiConsumer<Integer, Integer> sumAndPrint = (a, b) -> {int sum = a + b;System.out.println("Sum of " + a + " and " + b + " is: " + sum);};sumAndPrint.accept(10, 20); // 输出: Sum of 10 and 20 is: 30}
}
4.5、简化的接口引用
配Java 8中的Lambda表达式,在方法中简化引用。
public class MyClass {public void onEdgeConnect(String message) {System.out.println("Edge connected: " + message);}public void setup() {// 使用方法引用传递 onEdgeConnect 方法someMethod(this::onEdgeConnect);}public void someMethod(Consumer<String> callback) {// 模拟某个操作完成后调用回调callback.accept("Connection established");}
}