forked from javaevolved/javaevolved.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-chars-stream.yaml
More file actions
50 lines (50 loc) · 1.53 KB
/
string-chars-stream.yaml
File metadata and controls
50 lines (50 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
---
id: 35
slug: "string-chars-stream"
title: "String chars as stream"
category: "strings"
difficulty: "beginner"
jdkVersion: "9"
oldLabel: "Java 8"
modernLabel: "Java 9+"
oldApproach: "Manual Loop"
modernApproach: "chars() Stream"
oldCode: |-
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isDigit(c)) {
process(c);
}
}
modernCode: |-
str.chars()
.filter(Character::isDigit)
.forEach(c -> process((char) c));
summary: "Process string characters as a stream pipeline."
explanation: "String.chars() returns an IntStream of character values, enabling functional\
\ processing. For Unicode support, codePoints() handles supplementary characters\
\ correctly."
whyModernWins:
- icon: "🔗"
title: "Chainable"
desc: "Use filter, map, collect on character streams."
- icon: "📐"
title: "Declarative"
desc: "Describe what to do, not how to loop."
- icon: "🌐"
title: "Unicode-ready"
desc: "codePoints() correctly handles emoji and supplementary chars."
support:
state: "available"
description: "Available since JDK 8+ (improved in 9+)"
prev: "strings/string-formatted"
next: "strings/string-lines"
related:
- "strings/string-repeat"
- "strings/string-isblank"
- "strings/string-formatted"
docs:
- title: "String.chars()"
href: "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/String.html#chars()"
- title: "CharSequence.codePoints()"
href: "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/CharSequence.html#codePoints()"