# Use Stream::map

# Description

Extracts a block from the body of the consumer of the Stream::forEach method and introduces Stream::map instead. This makes complicated code blocks easier to read and reuse.

# Benefits

Arguably the lambda expression is easier to read and can be combined with other stream operations.

# Tags

# Code Changes

# Unwrapping single statement


    .filter(s -> !s.isEmpty())
    .forEach(s -> {
        String subString = s.substring(1);


    .filter(s -> !s.isEmpty())
    .map(s -> s.substring(1))
    .forEach(subString -> sb.append(subString));

You Want To Have Those Changes Done Automatically?

The automatic application of this rule is supported in the following jSparrow version:

# Properties

Property Value
Rule ID LambdaForEachMap
First seen in jSparrow version 2.0.0
Minimum Java version 8
Remediation cost 15 min