I have a List
:
List<String> mylist=Arrays.asList("Abla","Tbla","Cbla");
I have 2 filters prepared for my stream:
Predicate<String> startswith_A= s-> s.startsWith("A");
Predicate<String> startswith_T= s-> s.startsWith("T");
Following example filters for "A":
mylist.stream().filter(startswith_A).forEach(mystring -> {
System.out.println("filtered : " + mystring);
});
How can I apply the same for a filter "A" or "T"?
Something like:
mylist.stream().filter(startswith_A || startswith_T).forEach(mystring -> {
System.out.println("filtered : " + mystring);
});
I don't want to create a new predicate like:
Predicate<String> startswith_A_or_T= s-> s.startsWith("T") || s.startsWith("A");
How can I do this with 2 seperate Predicate
s?
You could simply use .or
between the 2 Predicates
:
mylist.stream()
.filter(startswith_A.or(startswith_T))
.forEach(mystring -> { System.out.println("filtered : " + mystring); });
Result:
filtered : Abla
filtered : Tbla
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With