Java 8 filter based on a boolean -
i want able apply filter based on boolean passed in.
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 make filter step based on ignoreeven flag. if true, ignore numbers. how go doing it? doing avoid code duplication
sounds straightforward or condition me.
.filter(number -> !ignoreeven || (number % 2 != 0))
Comments
Post a Comment