added updated example codes (#2062)

This commit is contained in:
Seun Matt
2017-06-13 11:58:53 +01:00
committed by Grzegorz Piwowarek
parent f73893bbb9
commit 53f4ec5f87
2 changed files with 41 additions and 20 deletions

View File

@@ -12,29 +12,40 @@ public class StringToCharStream {
public StringToCharStream() {
//let's use the Stream API to manipulate a string
//this will count the occurrence of each character in the test string
//let's use the Stream API to manipulate a string
//this will count the occurrence of each character in the test string
System.out.println("Counting Occurrence of Letter");
String testString = "Noww";
String testString = "tests";
//we don't want to use foreach, so . . .
//first get an IntStream
IntStream intStream = testString.chars();
IntStream intStream1 = testString.codePoints();
Map<Character, Integer> map = new HashMap<>();
//now let's map them
Stream<Character> characterStream = intStream.mapToObj(c -> (char) c);
Stream<Character> characterStream1 = intStream1.mapToObj(c -> (char) c);
testString.codePoints()
.mapToObj(c -> (char) c)
.filter(c -> Character.isLetter(c))
.forEach(c -> {
if(map.containsKey(c)) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);
}
});
System.out.println("Counting Occurrence of Letter");
testString = "Noww";
//we don't want to use foreach, so . . .
Map<Character, Integer> map = new HashMap<>();
testString.codePoints()
.mapToObj(c -> (char) c)
.filter(c -> Character.isLetter(c))
.forEach(c -> {
if(map.containsKey(c)) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);
}
});
//printing out the result here
System.out.println(map.toString());
//printing out the result here
System.out.println(map.toString());
}