2017-12-28 44 views
1

反応性のあるストリームを学習し、次のように2つのFluxを組み合わせようとしています。2つのFluxをzipWithを組み合わせて使用​​するとエラーが発生する

 List<Integer> elems = new ArrayList<>(); 
     Flux.just(10,20,30,40) 
      .log() 
      .map(x -> x * 2) 
      .zipWith(Flux.range(0, Integer.MAX_VALUE), 
        (two, one) -> String.format("First : %d, Second : %d \n", one, two)) 
      .subscribe(elems::add); 

と購読呼び出すときに、私は次のエラーました:

Multiple markers at this line 
    - The method subscribe(Consumer<? super String>) in the type Flux<String> is not applicable for the arguments 
    (elems::add) 
    - The type List<Integer> does not define add(String) that is applicable here 

をし、私は問題を解決するには、次の提案を得た:

enter image description here

しかし、これらの代替のどれを働いた。 提案、どのようにこの問題を解決するには?

答えて

2

時にはメソッド参照によって、明らかに見落とされることがあります。私はあなたの関数を書き直しましたが、匿名のクラスです。

List<Integer> elems = new ArrayList<>(); 
    Flux.just(10,20,30,40) 
     .log() 
     .map(x -> x * 2) 
     .zipWith(Flux.range(0, Integer.MAX_VALUE), 
      (two, one) -> String.format("First : %d, Second : %d \n", one, two)) 
     .subscribe(new Consumer<String>() { 
      @Override 
      public void accept(String s) { 

      } 
     }); 

この匿名クラスを作成するために私のIDE(intellij)からコード補完を使用しました。あなたが見ることができるように、この消費者への入力は、だから、あなたが使用してやろうとしているものであるList<Integer>からStringを追加することができないことを不平を言っている

String.format("First : %d, Second : %d \n", one, two) 

から来ている、Stringあるelems:add

関連する問題