从int -operator量数组中创建地图'%'不能应用于java.lang.object'
我有这样的代码,应该从整数数组中创建MAP
。 键表示数字的数量。
public static Map<Integer, List<String>> groupByDigitNumbersArray(int[] x) {
return Arrays.stream(x) // array to stream
.filter(n -> n >= 0) // filter negative numbers
.collect(Collectors.groupingBy(n -> Integer.toString((Integer) n).length(), // group by number of digits
Collectors.mapping(d -> (d % 2 == 0 ? "e" : "o") + d,
Collectors.toList()))); // if even e odd o add to list
}
问题在于mapping()
。 我遇到了一个错误:
Operator '%' cannot be applied to 'java.lang.Object', 'int'
有人知道如何解决这个问题吗?
I have code like this, which is supposed to create a Map
from an array of integers. The key represents the number of digits.
public static Map<Integer, List<String>> groupByDigitNumbersArray(int[] x) {
return Arrays.stream(x) // array to stream
.filter(n -> n >= 0) // filter negative numbers
.collect(Collectors.groupingBy(n -> Integer.toString((Integer) n).length(), // group by number of digits
Collectors.mapping(d -> (d % 2 == 0 ? "e" : "o") + d,
Collectors.toList()))); // if even e odd o add to list
}
The problem is in the line with mapping()
.
I'm getting an error:
Operator '%' cannot be applied to 'java.lang.Object', 'int'
Does someone know how to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
collect> collect()
期望collector
作为参数, oprive streams < /em>。即使没有模量运算符%
,您的代码也不会编译 - 评论 downstream collectorgroupingby()
以查看我在说什么关于。您需要应用
boxed()
操作,以便将intstream
转换为对象流stream&lt; integer&gt;
。您的方法可能看起来像这样:
我更改了
classifier
groupingby()
的功能更可读取。The flavor of
collect()
that expects aCollector
as an argument isn't available with primitive streams. Even without a modulus operator%
, your code will not compile - comment out the downstream collector ofgroupingBy()
to see what I'm talking about.You need to apply
boxed()
operation in order to convert anIntStream
into a stream of objectsStream<Integer>
.Your method might look like this:
I've changed the
classifier
function ofgroupingBy()
to be more readable.