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

Use a Java Refactoring Tool

No license required

You can review this refactoring on your code without a license by installing jSparrow to your Eclipse IDE. Install the plug-in from Eclipse IDE: Eclipse Marketplace.

System-wide Refactoring

Do you want to automate this refactoring (and many more) to your system-wide code? The automatic application of this system-wide refactoring can be unlocked by acquiring your jSparrow license.

a drawn cute bird pointing at a graph that shows positive results

# Properties

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