从int -operator量数组中创建地图'%'不能应用于java.lang.object'

发布于 2025-02-05 02:55:57 字数 746 浏览 3 评论 0原文

我有这样的代码,应该从整数数组中创建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 技术交流群。

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

发布评论

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

评论(1

金兰素衣 2025-02-12 02:55:57

collect> collect() 期望collector作为参数, oprive streams < /em>。即使没有模量运算符,您的代码也不会编译 - 评论 downstream collector groupingby()以查看我在说什么关于。

您需要应用boxed()操作,以便将intstream转换为对象流stream&lt; integer&gt;

您的方法可能看起来像这样:

public static Map<Integer, List<String>> groupByDigitNumbersArray(int[] x) {
    
    return Arrays.stream(x)  // creates a stream over the given array
        .filter(n -> n >= 0) // retain positive numbers and zero
        .boxed()             // <- converting IntStream into a Stream<Integer>
        .collect(Collectors.groupingBy(
            n -> String.valueOf(n).length(), // group by number of digits
            Collectors.mapping(d -> (d % 2 == 0 ? "e" : "o") + d, // if even concatinate 'e', if odd 'o'
                Collectors.toList()))); // collect to list
} 

我更改了classifier groupingby()的功能更可读取。

The flavor of collect() that expects a Collector as an argument isn't available with primitive streams. Even without a modulus operator %, your code will not compile - comment out the downstream collector of groupingBy() to see what I'm talking about.

You need to apply boxed() operation in order to convert an IntStream into a stream of objects Stream<Integer>.

Your method might look like this:

public static Map<Integer, List<String>> groupByDigitNumbersArray(int[] x) {
    
    return Arrays.stream(x)  // creates a stream over the given array
        .filter(n -> n >= 0) // retain positive numbers and zero
        .boxed()             // <- converting IntStream into a Stream<Integer>
        .collect(Collectors.groupingBy(
            n -> String.valueOf(n).length(), // group by number of digits
            Collectors.mapping(d -> (d % 2 == 0 ? "e" : "o") + d, // if even concatinate 'e', if odd 'o'
                Collectors.toList()))); // collect to list
} 

I've changed the classifier function of groupingBy() to be more readable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文