Reports Stream API chains, Iterable.forEach, and Map.forEach calls that can be automatically converted into classical loops.

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.

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);
  }

New in 2017.1