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

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -