GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing javaevolved.dev — String chars as stream | java.evolved
Strings
Beginner
String chars as streamProcess string characters as a stream pipeline. Code Comparison
✕ Java 8
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isDigit(c)) {
process(c);
}
}
✓ Java 9+
str.chars()
.filter(Character::isDigit)
.forEach(c -> process((char) c));
See a problem with this code? Let us know.
Why the modern way wins
🔗
ChainableUse filter, map, collect on character streams. 📐
DeclarativeDescribe what to do, not how to loop. 🌐
Unicode-readycodePoints() correctly handles emoji and supplementary chars. Old Approach
Manual Loop
Modern Approach
chars() Stream
Since JDK
9
Difficulty
Beginner
JDK Support
String chars as stream
Available
Available since JDK 8+ (improved in 9+) How it works
String.chars() returns an IntStream of character values, enabling functional processing. For Unicode support, codePoints() handles supplementary characters correctly. Related Documentation
Proof
Related patterns
Strings
Beginner
String.repeat()Java 8
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 3; i++) {
sb.append("abc");
}
String result = sb.toString();
Java 11+
String result = "abc".repeat(3); // "abcabcabc"
JDK 11+
→
Strings
Beginner
String.isBlank()Java 8
boolean blank =
str.trim().isEmpty();
// or: str.trim().length() == 0
Java 11+
boolean blank = str.isBlank(); // handles Unicode whitespace too
JDK 11+
→
Strings
Beginner
String.formatted()Java 8
String msg = String.format(
"Hello %s, you are %d",
name, age
);
Java 15+
String msg =
"Hello %s, you are %d"
.formatted(name, age);
JDK 15+
→
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |