Java 8 filters are based on Boolean values
•
Java
I want to be able to apply filters based on incoming Boolean values
public static List<Integer> multiplyNumbers(List<Integer> input,boolean ignoreEven){ return input.stream() .filter(number -> !(number%2==0)) .map(number -> number*2) .collect(Collectors.toList()); }
I want to filter according to the ignoreeven flag If true, ignore even numbers What should I do? I did this to avoid code duplication
Solution
It sounds simple or conditional to me
.filter(number -> !ignoreEven || (number % 2 != 0))
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码