Skip to content

Commit 53f4ec5

Browse files
SeunMattpivovarit
authored andcommitted
added updated example codes (eugenp#2062)
1 parent f73893b commit 53f4ec5

2 files changed

Lines changed: 41 additions & 20 deletions

File tree

‎core-java/src/main/java/com/baeldung/string/StringToCharStream.java‎

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,40 @@ public class StringToCharStream {
1212

1313
public StringToCharStream() {
1414

15-
//let's use the Stream API to manipulate a string
16-
//this will count the occurrence of each character in the test string
15+
//let's use the Stream API to manipulate a string
16+
//this will count the occurrence of each character in the test string
1717

18-
System.out.println("Counting Occurrence of Letter");
19-
String testString = "Noww";
18+
String testString = "tests";
2019

21-
//we don't want to use foreach, so . . .
20+
//first get an IntStream
21+
IntStream intStream = testString.chars();
22+
IntStream intStream1 = testString.codePoints();
2223

23-
Map<Character, Integer> map = new HashMap<>();
24+
//now let's map them
25+
Stream<Character> characterStream = intStream.mapToObj(c -> (char) c);
26+
Stream<Character> characterStream1 = intStream1.mapToObj(c -> (char) c);
2427

25-
testString.codePoints()
26-
.mapToObj(c -> (char) c)
27-
.filter(c -> Character.isLetter(c))
28-
.forEach(c -> {
29-
if(map.containsKey(c)) {
30-
map.put(c, map.get(c) + 1);
31-
} else {
32-
map.put(c, 1);
33-
}
34-
});
28+
System.out.println("Counting Occurrence of Letter");
29+
testString = "Noww";
30+
31+
//we don't want to use foreach, so . . .
32+
33+
Map<Character, Integer> map = new HashMap<>();
34+
35+
testString.codePoints()
36+
.mapToObj(c -> (char) c)
37+
.filter(c -> Character.isLetter(c))
38+
.forEach(c -> {
39+
if(map.containsKey(c)) {
40+
map.put(c, map.get(c) + 1);
41+
} else {
42+
map.put(c, 1);
43+
}
44+
});
45+
46+
//printing out the result here
47+
System.out.println(map.toString());
3548

36-
//printing out the result here
37-
System.out.println(map.toString());
3849
}
3950

4051

‎core-java/src/test/java/com/baeldung/string/StringToCharStreamUnitTest.java‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import java.util.stream.IntStream;
66
import java.util.stream.Stream;
77

8+
import static org.hamcrest.CoreMatchers.instanceOf;
89
import static org.junit.Assert.assertNotNull;
10+
import static org.junit.Assert.assertThat;
911
import static org.junit.Assert.assertTrue;
1012

1113
/**
@@ -17,12 +19,13 @@ public class StringToCharStreamUnitTest {
1719

1820
@Test
1921
public void givenTestString_whenChars_thenReturnIntStream() {
20-
assertTrue(testString.chars() instanceof IntStream);
22+
23+
assertThat(testString.chars(), instanceOf(IntStream.class));
2124
}
2225

2326
@Test
2427
public void givenTestString_whenCodePoints_thenReturnIntStream() {
25-
assertTrue(testString.codePoints() instanceof IntStream);
28+
assertThat(testString.codePoints(), instanceOf(IntStream.class));
2629
}
2730

2831
@Test
@@ -33,4 +36,11 @@ public void givenIntStream_whenMapToObj_thenReturnCharacterStream() {
3336
assertNotNull("IntStream returned by codePoints() did not map to Stream<Character>", characterStream1);
3437
}
3538

39+
@Test
40+
public void givenIntStream_whenMapToObj_thenReturnStringStream(){
41+
Stream<String> stringStream
42+
= testString.codePoints().mapToObj(c -> String.valueOf((char) c));
43+
assertNotNull(stringStream);
44+
}
45+
3646
}

0 commit comments

Comments
 (0)