String Lines And Blank Filtering

Split text into a stream of lines, strip whitespace, and drop blank lines with lines(), strip(), and isBlank() instead of manual splitting.

JavaCli Tooling

Code

snippet
import java.util.List;

public class LogCleaner {

    public static void main(String[] args) {
        String raw = """
                INFO  boot complete
                DEBUG trace noise

                   WARN  disk 81% full

                ERROR job failed once
                """;

        // lines() 按行终止符拆成流;strip() 去首尾空白;isBlank() 判定纯空白行
        List<String> cleaned = raw.lines()
                .map(String::strip)
                .filter(line -> !line.isBlank())
                .toList();

        cleaned.forEach(line -> System.out.println(line.toLowerCase()));

        String report = String.join(" | ", cleaned);
        System.out.println(report);
        System.out.println("=".repeat(report.length()));
        System.out.println("kept " + cleaned.size() + " of " + raw.lines().count() + " lines");
    }
}

How the snippet works

The working surface is the class LogCleaner and the function main — everything else is wiring.

Applying it

Running the file prints labeled results such as kept — each operation’s effect is visible in the output.

Statements that matter

.filter(line -> !line.isBlank())
.toList();

Interactive Tools for This Topic

FAQ

What does String Lines And Blank Filtering print when run?
Running the file directly is enough — expect labeled output lines such as `kept`.