java stream list of filters

  • A+

Let's say I've got a list of Strings and I want to filter them by the list of filtering Strings. For the list which consists of: "abcd", "xcfg", "dfbf"

I would precise list of filtering Strings: "a", "b", and after something like filter(i->i.contains(filterStrings) I would like to receive list of "abcd", "dfbf", And for a list of filtering Strings: "c", "f" I would like to reveive list of "xcfg" and "dfbf".

List<String> filteredStrings =             .filter(i -> i.contains("c") || i.contains("f")) //i want to pass a list of filters here             .collect(Collectors.toList()); 

Is there an other way of doing this instead of expanding body of lambda expression and writing a function with a flag which will check every filter?


You should instead be performing a anyMatch over the list to match from:

List<String> input = Arrays.asList("abcd", "xcfg", "dfbf"); // your input list Set<String> match = new HashSet<>(Arrays.asList("c", "f")); // to match from List<String> filteredStrings =         .filter(o ->         .collect(Collectors.toList()); 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: