【发布时间】:2021-07-12 10:48:19
【问题描述】:
我有一个字符串 s,我正在尝试使用 Stream API 将 s 中的每个字母映射到它在 s 中出现的次数。
例如,giraffe -> {1, 1, 1, 1, 2, 2, 1}。
我尝试使用的代码是:
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();
}
但我在以int[] occurences = ... 开头的行上得到incompatible types: possible lossy conversion from int to char
我尝试了一些不同的变化,但没有任何效果。有任何想法吗?谢谢
【问题讨论】:
标签: java lambda java-stream