小编man*_*pac的帖子

为什么需要将IntStream映射到Stream <Character>

  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。

java collections set java-8 java-stream

6
推荐指数
1
解决办法
116
查看次数

标签 统计

collections ×1

java ×1

java-8 ×1

java-stream ×1

set ×1