为什么我需要映射intstream才能流;

发布于 2025-02-09 05:25:03 字数 781 浏览 3 评论 0原文

  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;
    }

上面的代码不会没有:

.mapObj(c -> (char)c)
// <Character> Stream<Character> java.util.stream.IntStream.mapToObj(IntFunction<? extends Character> mapper)

如果我删除它,我会收到以下错误,

The method mapToInt((<no type> c) -> {}) is undefined for the type IntStream

有人可以解释这一点吗?好像我是从和intstream开始的,转换为字符流,然后回到intstream。

  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;
    }

The above code will not compile without:

.mapObj(c -> (char)c)
// <Character> Stream<Character> java.util.stream.IntStream.mapToObj(IntFunction<? extends Character> mapper)

If i remove it, I get the following error

The method mapToInt((<no type> c) -> {}) is undefined for the type IntStream

Can someone explain this? It seems like I am starting with and IntStream, converting to a Stream of Characters and then back to IntStream.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画中仙 2025-02-16 05:25:03

方法 chars 返回 intstream ,哪个当然不提供任何方法转换为int,例如maptoint,但maptoobj而不是。因此,方法 intstream :: map(intunaryAryAperator mapper) ,这两者都应使用返回int,因为 intunaryoperator函数&lt; integer,integer&gt;UnaryAryPerator&lt; integer&gt;

int count = myString.chars()                 // IntStream
    .map(c -> (set.add((char) c) ? 1 : 0))   // IntStream
    .sum();

long count = myString.chars()                // IntStream
    .filter(c -> set.add((char) c))          // IntStream
    .count();

另外,使用set set&lt; integer&gt; 可帮助您避免转换到角色:但是,无论如何,无论如何,无论如何

Set<Integer> set = new HashSet<>();

int count = myString.chars()                 // IntStream
    .map(c -> (set.add(c) ? 1 : 0))          // IntStream
    .sum();

long count = myString.chars()                // IntStream
    .filter(set::add)                        // IntStream
    .count();

,无论如何,无论如何,无论如何您尝试实现的目标,原则上的代码是错误的。请参阅无统计行为 。考虑使用以下片段,其中lambda表达式的结果不是依赖性,例如set> set :: add

流管线结果可能是不确定性的,或者如果流动的行为参数状态是不正确的。

long count = myString.chars()             // IntStream
                     .distinct()          // IntStream
                     .count();

The method CharSequence::chars returns the IntStream, which of course doesn't provide any method converting to int, such as mapToInt, but mapToObj instead. Therefore the method IntStream::map(IntUnaryOperator mapper) which both takes returns int as well shall be used since IntUnaryOperator does the same like Function<Integer, Integer> or UnaryOperator<Integer>:

int count = myString.chars()                 // IntStream
    .map(c -> (set.add((char) c) ? 1 : 0))   // IntStream
    .sum();

long count = myString.chars()                // IntStream
    .filter(c -> set.add((char) c))          // IntStream
    .count();

Also, using Set<Integer> helps you to avoid conversion to a Character:

Set<Integer> set = new HashSet<>();

int count = myString.chars()                 // IntStream
    .map(c -> (set.add(c) ? 1 : 0))          // IntStream
    .sum();

long count = myString.chars()                // IntStream
    .filter(set::add)                        // IntStream
    .count();

However, regardless of what you try to achieve, your code is wrong by principle. See the Stateless behaviors. Consider using the following snippet which lambda expressions' results are not dependent on the result of a non-deterministic operation, such as Set::add.

Stream pipeline results may be nondeterministic or incorrect if the behavioral parameters to the stream operations are stateful.

long count = myString.chars()             // IntStream
                     .distinct()          // IntStream
                     .count();
追我者格杀勿论 2025-02-16 05:25:03

您还可以收集到集合,然后在不使用显式地图的情况下进行大小。
它不需要使用外部状态包含字符。

    long count = str.chars().boxed().collect(Collectors.toSet()).size();

但是恕我直言,已经提到的更直接的方法在外观上更干净,我希望使用这种方法。

    long count = str.chars().distinct().count();

You can also collect to a set and then take the size without using an explicit map.
It does not require using external state to contain the characters.

    long count = str.chars().boxed().collect(Collectors.toSet()).size();

But imho, the more direct approach which was already mentioned is cleaner in appearance and the one I would prefer to use.

    long count = str.chars().distinct().count();
如歌彻婉言 2025-02-16 05:25:03

因为string.chars()已经返回intstreamintstream没有maptoint function < /code>

您可以使用过滤器,然后计数:

int count = myString.chars()
      .filter(c -> set.add(c) == true)
      .count();

我承认我在上个午夜做了这么懒惰!
如评论所述,这是所需的修复程序。

谢谢您提及。

long count = myString.chars()
          .filter(c -> set.add((char)c))
          .count();

Because String.chars() is already returning an IntStream and IntStream does not have mapToInt function

You could use a filter instead then count:

int count = myString.chars()
      .filter(c -> set.add(c) == true)
      .count();

I admit that I made this so slubby last midnight!
As mentioned by the comments, here is the required fixes.

Thank you for mentioning.

long count = myString.chars()
          .filter(c -> set.add((char)c))
          .count();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文