I've got a string s
, and I'm trying to map each letter in s
to the number of times it appears in s
, using the Stream API.
For example, giraffe -> {1, 1, 1, 1, 2, 2, 1}
.
The code I've tried using is:
String s = "giraffe";
int[] occurences = s.chars().map(c -> count(s, c)).toArray();
public int count(String text, char ch) {
return (int) text.chars().filter(c -> c == ch).count();
}
but I get incompatible types: possible lossy conversion from int to char
on the line that starts with int[] occurences = ...
I've tried some different variations but nothing has worked. Any ideas? Thanks