According to its JavaDocs, java.util.Stream.peek()
“exists mainly to support debugging” purposes. Although this does not mean that
using it for other purposes is discouraged, relying on peek()
without careful consideration can lead to error-prone code such as:
peek()
action will not be
invoked at all. peek()
action will be invoked for fewer elements
or not at all. This rule raises an issue for each use of peek()
to be sure that it is challenged and validated by the team to be meant for production
debugging/logging purposes.
Stream.of("one", "two", "three", "four") .filter(e -> e.length() > 3) .peek(e -> System.out.println("Filtered value: " + e)); // Noncompliant