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?
CodePudding user response:
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.
CodePudding user response:
You have to secure that the elements of the array you are working on is a Reference type and not a primitive type. So you have two options:
Convert your argument int[ ] x to an Integer[ ] or
public static Map<Integer, List<String>> groupByDigitNumbersArray(int[] x){ return Arrays.stream(y) //array to stream .filter(n -> n >= 0) //filter negative numbers .boxed() //int stream to wrapper Integer stream .collect(Collectors.groupingBy(n -> n.toString().length(), //group by number of digits mapping(d -> (d % 2 == 0 ? "e" : "o") d, Collectors.toList()))); //if even e odd o add to list }
Change the signatur of your methode to Integer[ ]
public static Map<Integer, List<String>> groupByDigitNumbersArray(Integer[] x){ return Arrays.stream(x) //array to stream .filter(n -> n >= 0) //filter negative numbers .collect(Collectors.groupingBy(n -> n.toString().length(), //group by number of digits mapping(d -> (d % 2 == 0 ? "e" : "o") d, Collectors.toList()))); //if even e odd o add to list }