Inspectopedia 2025.3 Help

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. This can help to downgrade for backward compatibility with earlier Java versions.

Example:

String joinNonEmpty(List<String> list) { return list.stream() // Stream can be converted to loop .filter(s -> !s.isEmpty()) .map(String::trim) .collect(Collectors.joining(", ")); }

After the quick-fix is applied:

String joinNonEmpty(List<String> list) { StringJoiner joiner = new StringJoiner(", "); for (String s : list) { if (!s.isEmpty()) { String trim = s.trim(); joiner.add(trim); } } return joiner.toString(); }

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.

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

StreamToLoop
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Java | Code style issues

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:

List<ProcessHandle> handles = ProcessHandle.allProcesses().collect(Collectors.toList());

In this case, the result will be as follows:

List<ProcessHandle> handles = new ArrayList<>(); for (Iterator<ProcessHandle> it = ProcessHandle.allProcesses().iterator(); it.hasNext(); ) { ProcessHandle allProcess = it.next(); handles.add(allProcess); }

This inspection depends on the Java feature 'Stream and Optional API', which is available since Java 8.

Inspection ID: StreamToLoop

New in 2017.1

Inspection options

Here you can find the description of settings available for the Stream API call chain can be replaced with loop inspection, and the reference of their default values.

Iterate unknown Stream sources via Stream.iterator()

Option ID:

SUPPORT_UNKNOWN_SOURCES

Default value:

Not selected

Suppressing Inspection

You can suppress this inspection by placing the following comment marker before the code fragment where you no longer want messages from this inspection to appear:

//noinspection StreamToLoop

More detailed instructions as well as other ways and options that you have can be found in the product documentation:

Inspection Details

By default bundled with:

IntelliJ IDEA 2025.3, Qodana for JVM 2025.3,

Last modified: 03 December 2025