I have an implementation to find duplicate characters in a String:
Map<Character, Integer> charMap = new HashMap<>();
for (int i = 0; i < word.length(); i++) {
Character ch = word.charAt(i);
if (charMap.containsKey(ch)) {
charMap.put(ch, charMap.get(ch) + 1);
} else {
charMap.put(ch, 1);
}
}
I want to know if I can convert this implementation into an Java 8 and Stream implementation. To do something like this:
Map<Character, Integer> charMap = new HashMap<>();
Then convert the String word into a List of characters:
List<Character> chars = word.chars()
.mapToObj(i -> (char) i)
.collect(Collectors.toList());
And now to do something like that:
chars.stream()
.filter(e -> charMap.containsKey(e))...