This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 247772

Summary: "Hints > General > Use Functional Operations" should not attempt to provide a solution
Product: java Reporter: terje7601
Component: HintsAssignee: Svata Dedic <sdedic>
Status: RESOLVED WONTFIX    
Severity: normal    
Priority: P3    
Version: 8.0.1   
Hardware: PC   
OS: Windows 7   
Issue Type: DEFECT Exception Reporter:

Description terje7601 2014-10-07 13:56:36 UTC
It's extremely hard for NetBeans (or any tool) to provide automatic conversion of for loops which results in optimal Streams API code. Sometimes it even results in broken code. So in my opinion, I think this hint should not attempt to provide a solution at all. Moreover, this hint will lead to users blindly converting all of their for-loops, complaining about the verbosity of the result, not realizing they ought to use ".collect(toList())" and the likes, introducing subtle concurrency bugs, ... in short: not learning to use the Streams API correctly.
In my opinion, the best NetBeans can do is to link to the relevant javadoc, e.g. http://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html or http://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html

For example, consider this snippet:

List<String> input = Arrays.asList("1", "2");
List<Integer> output = new ArrayList<>();
for(String i : input) {
    output.add(i.length());
}

NetBeans converts it to:

List<String> input = Arrays.asList("1", "2");
List<Integer> output = new ArrayList<>();
input.stream().forEach((i) -> {
    output.add(i.length());
});

While the optimal solution is:

List<String> input = Arrays.asList("1", "2");
List<Integer> output = input.stream().map(String::length).collect(toList());

Moreover, if someone later changes the NetBeans-converted code to use parallelStream() instead of stream(), the code is broken because multiple threads will modify "output" without synchronization.
Comment 1 Svata Dedic 2016-06-21 21:06:35 UTC
I agree that it is hard to transform the code right. The user may turn the 'Use functional operations' hint off.