Stream API call chain can be replaced with loop
Reports Stream API chains, Iterable.forEach()
, and Map.forEach()
calls that can be automatically converted into classical loops.
Example:
After the quick-fix is applied:
Note that sometimes this inspection might cause slight semantic changes. Special care should be taken when it comes to short-circuiting, as it's not specified how many elements will be actually read when the stream short-circuits.
Stream API appeared in Java 8. This inspection can help to downgrade for backward compatibility with earlier Java versions.
Configure the inspection:
Use the Iterate unknown Stream sources via Stream.iterator() option to suggest conversions for streams with unrecognized source. In this case, iterator will be created from the stream. For example, when checkbox is selected, the conversion will be suggested here:
In this case, the result will be as follows:
New in 2017.1
Inspection options
Option | Type | Default |
---|---|---|
Iterate unknown Stream sources via Stream.iterator() | Checkbox | false |
Inspection Details | |
---|---|
Available in: | IntelliJ IDEA 2023.3, Qodana for JVM 2023.3 |
Plugin: | Java, 233.SNAPSHOT |