2017-09-19 7 views
0

私は1つのフラックスに異なる種類のオブジェクトがあり、その中にタイプ別にグループ要素を入れて、それぞれのグループを減らしたい。反応器内のGroupedFluxを減らす

ここにこれを行う簡単なコードです。

Flux<Item> item=Flux.just(new Item("item1"), new Item("item2")); 
    Flux<Item2> item2=Flux.just(new Item2(4.0f), new Item2(2.0f)); 

    Flux<Object> objects=Mono.empty().concatWith(item).concatWith(item2);  
    Flux<GroupedFlux<Object>> groups=objects.groupBy(value -> value.getClass()); 

どのように種類ごとにフローを減らすことができますか? enter code here

答えて

2

私はぐずぐずして解決策を見つけました。 reactiviyのために@simonbasleと@osiに感謝します。

https://gitter.im/reactor/reactor

キーは、メソッドflatMapし、値をdiscrimineキーを使用します。

groups.flatMap(gf -> { 
     if(gf.key()==Item.class) { 
      return gf.reduce(new Item(""), (a, b) -> { 
       return new Item(a.getValue()+((Item)b).getValue()); 
      }); 
     } 
     else if(gf.key()==Item2.class) { 
      return gf.reduce(new Item2(0.0f), (a, b) -> { 
       return new Item2(a.getValueF()+((Item2)b).getValueF()); 
      }); 
     } 
     throw new IllegalArgumentException("unknown key: "+gf.key()); 
    }); 
関連する問題