5

これは理論上の問題であり、具体的な応用はありません。

私は触れない次の方法を持っています。(可能であれば) として使用できBiConsumerます。

void doSmallThing(A a, B b) {
  // do something with a and b.
}

void doBigThing(List<A> as, B b) {
  // What to do?
}

一定にas保ちながら繰り返し使用するにはどうすればよいですか?bthis::doSmallThingdoBigThing

もちろん、以下は機能しません。

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ますか?

4

3 に答える 3

1

BiConsumers は、Map エントリを反復処理するときに使用されます。たとえば、次のようになります。

Map<A, B> map = ...;
map.forEach(this::doSomething);

Stream.collect()も BiConsumers を引数として取りますが、マップ エントリの反復よりも使用頻度は低くなります。

于 2015-06-25T10:18:16.007 に答える