I have a static method which takes the parameter Stream<Double> stream. Coming from either a arraylist.stream() or Arrays.stream(array).
The method's job is to return the sum of all integers that are divisible by three.
return stream.filter(i -> i.intValue() % 3 == 0).mapToInt(i -> i.intValue()).sum()
This method works, however IntelliJ is suggesting the following:
This inspection reports lambdas which can be replaced with method references.
I'm not overly familiar with method references, especially the referencing instance methods using class names scenario.
I have tried the following which gives an error.
stream.filter(i -> i.intValue() % 3 == 0).mapToInt(Integer::intValue).sum()
Any suggestions?
iis a Double, you should useDouble::intValue. IntelliJ not only inspects, but also proposes the change. Just hit alt-enter..mapToInt(Double::intValue).filter(i -> i % 3 == 0)i.intValue() % 3 == 0, already does exactly that.