how to force Java 8 steams execute sequentially?

arminvanbuuren :

I have a Stream as method's argument. I don't know if it parallel or not. How can I guarantee sequential execute it?

(no if/else/instance of pls)

Thanks

Eugene :

wait... just invoke sequential() on it?

This is just a flag so the last invocation of sequential/parallel wins, that means even if your stream is parallel and you receive it as a method argument, just invoke sequential anywhere before the terminal operation and you are done

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=85677&siteId=1