John Taylor
John Taylor

Reputation: 35

How can I use OR in filters with Speedment

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

Answers (1)

Per-Åke Minborg
Per-Åke Minborg

Reputation: 290

In this particular case there are at least two ways to go: a) use Predicate::or b) Use an in predicate

Predicate::or

users.stream()
    .filter(
        User.STATE.equals("NY")
        .or(User.STATE.equals("NJ"))
        .or(User.STATE.equals("PA"))
    )
    .collect(Collectors.toList());

in() Predicate

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

Related Questions