Search code examples
javaarraysjava-streamcollectors

Creating a Map from an Array of int - Operator '%' cannot be applied to 'java.lang.Object'


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?


Solution

  • 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.