I have a stream that emit pages of data A, B, C, D.... Which operator can I use to get a stream like this: [A], [A, B], [A, B, C], [A, B, C, D]...?
I know collect or toList but they only emit once at the end of the stream.
You can try the 'scan' operator.
Java 8
You could get a Stream of String[] in this way:
List<String> array = Arrays.asList("A","B","C","D");
String[] st ={""};
Stream<String[]> stream = array.stream().map(x -> (st[0]+=x).split("\\B"));
//print it
stream.forEach(s -> System.out.println(Arrays.toString(s)));
RX JAVA (expanding JohnWowUs's answer)
List<String> array = Arrays.asList("A","B","C","D");
Func2<String,String,String> function = new Func2<String,String,String>(){
@Override
public String call(String paramT1, String paramT2) {
return paramT1 + paramT2;
}
};
Observable<String> scan = Observable.from(array).scan(function);
scan.forEach(x-> System.out.println(x));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With