public static int construction(String myString) {
Set<Character> set = new HashSet<>();
int count = myString.chars() // returns IntStream
.mapToObj(c -> (char)c) // Stream<Character> why is this required?
.mapToInt(c -> (set.add(c) == true ? 1 : 0)) // IntStream
.sum();
return count;
}
Run Code Online (Sandbox Code Playgroud)
如果没有以下条件,以上代码将无法编译:
.mapObj(c -> (char)c)
// <Character> Stream<Character> java.util.stream.IntStream.mapToObj(IntFunction<? extends Character> mapper)
Run Code Online (Sandbox Code Playgroud)
如果删除它,会出现以下错误
The method mapToInt((<no type> c) -> {}) is undefined for the type IntStream
Run Code Online (Sandbox Code Playgroud)
有人可以解释吗?似乎我从IntStream开始,转换为字符流,然后返回IntStream。