Search code examples
javaspeedment

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...


Solution

  • 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.