java - What is difference between Collection.stream().forEach() and Collection.forEach()? -


I understand that With stream () , I can use chain operations like . Use filter () or parallel stream. But if there is a need to perform small operations (for example, printing of elements of the list), then what is the difference between them?

  collection.stream (). ForEach (System.out :: println); Collection.forEach (System.out :: println); For ordinary cases such as an illustrated, they are mostly the same.    

However, there are many subtle differences in this that can be important.

With a problem sequence, with Stream.forEach , the command is undefined . It is unlikely to be sequential streams, however, it is within the specification of stream.for you to execute in some arbitrary order. It is often in parallel currents, on the contrary, Iterable.forEach is always executed in the order of running renewable , if any is specified.

Another issue with side effects is the non-interference task specified in Stream.forEach (see.) Iterable ForEach is likely to have fewer restrictions for the collection in java.util , Iterable.forEach usually Iterator of that archive , Most of which are designed and which have concurrent improvement concept if the archive is structurally repainted during the repetition Dit is. However, amendments that are not structural are allowed during the run For example, "Setting the value of only an element is not structural modification." Thus, the action for ArrayList.forEach is allowed to set the value of the underlying ArrayList without problems.

Concurrent collections are different again, instead of fast-paced, they are ready to be fully defined on that link. In essence, however, consider ConcurrentLinkedDeque . Your forEach method is is allowed to modify the built-in deck, even structurally, and concurrent promotion exceptions Never thrown out, however, that revision may or may not be visible in this recycling. (Therefore "weak" compatibility.)

Yet another difference is visible if Iterable.forEach is running on a synchronized archive. On such a collection, Iterable.forEach puts it once and in all calls in the verb method. The stream.EEAC uses the call collection descriptor, which does not lock, and which depends on the prevalent rule of non-interference. Collections that support the stream can be modified during the recurrence, and if so, then there may be a result of a concurrent modification exception or incompatible behavior.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -