Please use the Apache issue tracking system for new NetBeans issues (https://issues.apache.org/jira/projects/NETBEANS0/issues) !!
Bug 257129 - Converting enhanced-for to forEach via "use functional operations" hint is incorrect
Converting enhanced-for to forEach via "use functional operations" hint is in...
Status: RESOLVED FIXED
Product: java
Classification: Unclassified
Component: Hints
8.1
PC Windows 8.1
: P2 (vote)
: 8.2
Assigned To: Svata Dedic
issues@java
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2015-12-13 21:04 UTC by jbosboom
Modified: 2016-05-25 01:50 UTC (History)
0 users

See Also:
Issue Type: DEFECT
:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description jbosboom 2015-12-13 21:04:23 UTC
The "use functional operations" hint will convert

List<Object> target = new ArrayList();
for (Object o : collection)
    target.add(o);

into

collection.stream().forEach((o) -> {
    target.add(o);
});

but this is incorrect because Stream.forEach does not preserve encounter order, possibly leading to the elements being added out of order.  The Stream.forEach Javadoc is mildly ambiguous, but Brian Goetz has confirmed that Stream.forEach does not preserve encounter order even for sequential streams in the answer to this Stack Overflow question: http://stackoverflow.com/q/34247318/3614835

Iterable.forEach does preserve order, as does Stream.forEachOrdered, so the hint should be changed to use one of those two.
Comment 1 Svata Dedic 2016-05-20 11:41:03 UTC
Increasing priority; such transformation may create hard to spot bugs - ideally surfacing with some new JDK version or fancy collection which really cares about stream performance.

I am tempted, ;) for your this very case, to change NB's behaviour to rewrite to collection.forEach(target::add) ... which will rule out streams altogether but that obviously does not solve the more complicated ones.
Comment 2 Svata Dedic 2016-05-23 15:23:36 UTC
I've fixed this issue. After a LOT of thinking I just changed forEach into forEachOrdered without adding much of logic and filed an issue #262189, which seems to take a LOT of time to fix.
I fixed just some little but annoing bugs this time.

Committed as jet-main#035ef8f5311b
Comment 3 Quality Engineering 2016-05-25 01:50:45 UTC
Integrated into 'main-silver', will be available in build *201605250002* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)

Changeset: http://hg.netbeans.org/main-silver/rev/035ef8f5311b
User: Svata Dedic <sdedic@netbeans.org>
Log: #257129: use forEachOrdered to maintain iteration order; fixed linearization when a statement follows a nested block.


By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo