RFR: 8273349: Check uses of Stream::peek in controls and replace as needed [v2]
Karthik P K
kpk at openjdk.org
Thu Mar 28 06:11:34 UTC 2024
On Wed, 27 Mar 2024 23:24:51 GMT, Marius Hanl <mhanl at openjdk.org> wrote:
>>> In the java.util.stream package [docs](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/stream/package-summary.html#SideEffects) it is mentioned that `forEach()` method operates only via side-effects. So do you think we should avoid using `forEach()` here and iterate the generated list separately to clear selected index?
>>
>> `forEach` is used correctly here. From the docs:
>>> With the exception of terminal operations [forEach](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/stream/Stream.html#forEach(java.util.function.Consumer)) and [forEachOrdered](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/stream/Stream.html#forEachOrdered(java.util.function.Consumer)), side-effects of behavioral parameters may not always be executed when the stream implementation can optimize away the execution of behavioral parameters without affecting the result of the computation.
>>
>>> Another idea is to use `toList()`, which is a very efficient operation and then iterate over it.
>>
>> That's still 2 iterations. If the code is not performance-critical it doesn't matter.
>
>> That's still 2 iterations.
>
> Yes, but one advantage here:
> We currently do `final List<Integer> removed = new ArrayList<>(c.getRemovedSize());`,
> where we allocate a list with a size, that is probably too big since we filter the removed items.
> So with `toList`, we at least get back a list with the correct size.
> But true, that we technically iterate twice then. It probably does not matter too much.
> `forEach` is used correctly here. From the docs:
>
> > With the exception of terminal operations [forEach](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/stream/Stream.html#forEach(java.util.function.Consumer)) and [forEachOrdered](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/stream/Stream.html#forEachOrdered(java.util.function.Consumer)), side-effects of behavioral parameters may not always be executed when the stream implementation can optimize away the execution of behavioral parameters without affecting the result of the computation.
>
Thanks for pointing this out.
-------------
PR Review Comment: https://git.openjdk.org/jfx/pull/1430#discussion_r1542359758
More information about the openjfx-dev
mailing list