为什么我需要映射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;
}
上面的代码不会没有:
.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
方法 chars 返回
intstream
,哪个当然不提供任何方法转换为int,例如maptoint
,但maptoobj
而不是。因此,方法intstream :: map(intunaryAryAperator mapper)
,这两者都应使用返回int
,因为intunaryoperator
函数&lt; integer,integer&gt;
或UnaryAryPerator&lt; integer&gt;
:另外,使用
set set&lt; integer&gt;
可帮助您避免转换到角色:但是,无论如何,无论如何,无论如何,无论如何,无论如何,无论如何您尝试实现的目标,原则上的代码是错误的。请参阅无统计行为 。考虑使用以下片段,其中lambda表达式的结果不是依赖性,例如
set> set :: add
。The method
CharSequence::chars
returns theIntStream
, which of course doesn't provide any method converting to int, such asmapToInt
, butmapToObj
instead. Therefore the methodIntStream::map(IntUnaryOperator mapper)
which both takes returnsint
as well shall be used sinceIntUnaryOperator
does the same likeFunction<Integer, Integer>
orUnaryOperator<Integer>
:Also, using
Set<Integer>
helps you to avoid conversion to a Character: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
.您还可以收集到集合,然后在不使用显式地图的情况下进行大小。
它不需要使用外部状态包含字符。
但是恕我直言,已经提到的更直接的方法在外观上更干净,我希望使用这种方法。
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.
But imho, the more direct approach which was already mentioned is cleaner in appearance and the one I would prefer to use.
因为
string.chars()
已经返回intstream
和intstream
没有maptoint
function < /code>
您可以使用过滤器,然后计数:
我承认我在上个午夜做了这么懒惰!
如评论所述,这是所需的修复程序。
谢谢您提及。
Because
String.chars()
is already returning anIntStream
andIntStream
does not havemapToInt
function
You could use a filter instead then 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.