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.
Code
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
Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.Tar Archive Command BuilderBuild safe tar create, extract, and list commands for gzip, xz, zstd, and bzip2 archives with excludes, target directory (-C), and flag explanations.
FAQ
- What does String Lines And Blank Filtering print when run?
- Running the file directly is enough — expect labeled output lines such as `kept`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.