BiConsumer示例

BiConsumer示例

在java8中,BiConsumer是一个函数接口,它接收两个参数,没有返回值

@FunctionalInterface
public interface BiConsumer<T, U> {
  void accept(T t, U u);
}

BiConsumer

import java.util.function.Consumer;

public class JavaBiConsumer1 {

    public static void main(String[] args) {

      BiConsumer<Integer, Integer> addTwo = (x, y) -> System.out.println(x + y);
      addTwo.accept(1, 2);    // 3
    }
}

输出

3

高阶函数

下面示例接收BiConsumer作为参数,创建通用的addTwo来连接两个对象。

import java.util.function.BiConsumer;

public class JavaBiConsumer2 {

    public static void main(String[] args) {

        addTwo(1, 2, (x, y) -> System.out.println(x + y));          // 3
        addTwo("Node", ".js", (x, y) -> System.out.println(x + y)); // Node.js

    }

    static <T> void addTwo(T a1, T a2, BiConsumer<T, T> c) {
        c.accept(a1, a2);
    }
}

输出

3
Node.js

更多示例

import java.util.function.BiConsumer;

public class JavaBiConsumer3 {

    public static void main(String[] args) {

        math(1, 1, (x, y) -> System.out.println(x + y));   // 2
        math(1, 1, (x, y) -> System.out.println(x - y));   // 0
        math(1, 1, (x, y) -> System.out.println(x * y));   // 1
        math(1, 1, (x, y) -> System.out.println(x / y));   // 1

    }

    static <Integer> void math(Integer a1, Integer a2, BiConsumer<Integer, Integer> c) {
        c.accept(a1, a2);
    }
}

输出

2
0
1
1

Map.foreach

在JDK的源代码中,Map.forEach接收BiConsumer作为参数

Map.java

default void forEach(BiConsumer<? super K, ? super V> action) {
        Objects.requireNonNull(action);
        for (Map.Entry<K, V> entry : entrySet()) {
            K k;
            V v;
            try {
                k = entry.getKey();
                v = entry.getValue();
            } catch (IllegalStateException ise) {
                // this usually means the entry is no longer in the map.
                throw new ConcurrentModificationException(ise);
            }
            action.accept(k, v);
        }
    }

JavaMapBiConsumer.java

import java.util.LinkedHashMap;
import java.util.Map;

public class JavaMapBiConsumer {

    public static void main(String[] args) {

        Map<Integer, String> map = new LinkedHashMap<>();

        map.put(1, "Java");
        map.put(2, "C++");
        map.put(3, "Rust");
        map.put(4, "JavaScript");
        map.put(5, "Go");

        map.forEach((k, v) -> System.out.println(k + ":" + v));

    }
}

输出

1:Java
2:C++
3:Rust
4:JavaScript
5:Go
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BiConsumer是一个函数式接口,它接受两个输入参数并不返回任何结果。下面是一个使用Java 8的BiConsumer的简单示例: 假设我们有一个名为Person的类,它具有两个属性:name和age。我们想要在控制台上打印出每个人的姓名和年龄。我们可以使用BiConsumer接口来实现这个功能,如下所示: ```java import java.util.function.BiConsumer; public class BiConsumerExample { public static void main(String[] args) { BiConsumer<Person, String> printName = (person, prefix) -> System.out.println(prefix + " " + person.getName()); BiConsumer<Person, Integer> printAge = (person, prefix) -> System.out.println(prefix + " " + person.getAge()); Person person1 = new Person("Alice", 25); Person person2 = new Person("Bob", 30); printName.andThen(printAge).accept(person1, "Person 1:"); printName.andThen(printAge).accept(person2, "Person 2:"); } } class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } } ``` 在上面的示例中,我们定义了两个BiConsumer实例:printName和printAge。printName将接受Person对象和一个前缀字符串,并打印出该人的姓名,printAge将接受Person对象和一个前缀字符串,并打印出该人的年龄。 我们创建了两个Person对象,person1和person2,然后使用andThen方法将两个BiConsumer实例链接起来,这样我们就可以一次性打印出每个人的姓名和年龄。最后,我们调用accept方法,将每个Person对象和前缀字符串传递给BiConsumer实例。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值