Reputation: 35
How can I use OR when I want to filter Speedment streams? How do I write a stream with users that are either from NY, NJ or PA?
users.stream()
.filter(User.STATE.equals("NY"))
.collect(Collectors.toList())
This produces a list of users from NY only...
Upvotes: 2
Views: 77
Reputation: 290
In this particular case there are at least two ways to go:
a) use Predicate::or
b) Use an in
predicate
users.stream()
.filter(
User.STATE.equals("NY")
.or(User.STATE.equals("NJ"))
.or(User.STATE.equals("PA"))
)
.collect(Collectors.toList());
users.stream()
.filter(User.STATE.in("NY", "NJ", "PA"))
.collect(Collectors.toList());
I would use the latter solution which looks nicer in my opinion.
Upvotes: 1