Java 8 lambda api
•
Java
I'm trying to migrate from RX java to Java 8 Lambdas An example I can't find is the method of buffering requests For example, in Rx Java, I can say the following
Observable.create(getIterator()).buffer(20,1000,TimeUnit. MILLISECONDS).doOnNext(list -> doWrite(list));
We buffer 20 elements into a list, or timeout for 1000 milliseconds, which occurs first
Observables in Rx are observable "push" styles, where streams uses Java pull Is it possible to implement my own map operation in the stream, or because doonnext must poll the previous element, otherwise it cannot issue the cause of the problem?
Solution
One way is to use BlockingQueue and guava Use queues Drain, you can create a collection, then call stream () and transform it. Here's a link: guava queues drain
Here is a simple example:
public void transform(BlockingQueue<Something> input) { List<Something> buffer = new ArrayList<>(20); Queues.drain(input,buffer,20,TimeUnit.MILLISECONDS); doWrite(buffer); }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码