String chars as stream
Process 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));
Why the modern way wins
Chainable
Use filter, map, collect on character streams.
Declarative
Describe what to do, not how to loop.
Unicode-ready
codePoints() 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.