# 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

Pre

list.stream()
    .filter(s -> !s.isEmpty())
    .forEach(s -> {
        String subString = s.substring(1);
        sb.append(subString);
    });

Post

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

🛠️ Auto-refactor Available

You can auto-refactor this with jSparrow.
Drop this button to your Eclipse IDE workspace to install jSparrow for free:

Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client

Need help? Check out our installation guide.

# Properties

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