这只是一个理论问题,没有具体应用。
我有以下方法,我不会碰。它可以(如果可能的话)用作BiConsumer
.
void doSmallThing(A a, B b) {
// do something with a and b.
}
void doBigThing(List<A> as, B b) {
// What to do?
}
如何as
在保持b
不变并使用this::doSmallThing
in的同时进行迭代doBigThing
?
当然,以下不起作用。
void doBigThing(List<A> as, B b) {
as.stream()
.forEach(this::doSmallThing);
}
以下效果很好,实际上是我每天使用的。
void doBigThing(List<A> as, B b) {
as.stream()
.forEach(a -> doSmallThing(a, b));
}
以下也很好用,但有点棘手。
Consumer<A> doSmallThingWithFixedB(B b) {
return (a) -> doSmallThing(a, b);
}
void doBigThing(List<A> as, B b) {
as.stream()
.forEach(doSmallThingWithFixedB(b))
}
但是所有这些解决方案都没有得到Consumer
案例的简单性。那么有什么简单的存在BiConsumer
吗?