检测 Stream API 链之外的可以合并到其中的转换。

示例:


  List<String> list = stream.collect(Collectors.toList());
  list.sort(null);
  return list.toArray(new String[list.size()]);

转换后:


  return stream.sorted().toArray(String[]::new);

请注意,有时转换后的 Stream 链可能会将显式 ArrayList 替换为 Collectors.toList() 或将显式 HashSet 替换为 Collectors.toSet()。 当前库实现会在内部使用这些集合。 但是,这种方法不是很可靠,将来可能会更改代码的语义。

如果您对此感到担心,请使用不建议使用 'toList()' 或 'toSet()' 收集器选项来建议使用 Collectors.toCollection() 而不是 toListtoSet 收集器。

仅当项目或模块的语言级别为 8 级或更高级别时,该检查才会进行报告。