Java 8 Streams – Filter More Than One Condition

We Are Going To Discuss About Java 8 Streams – Filter More Than One Condition. So lets Start this Java Article.

Java 8 Streams – Filter More Than One Condition

Advertisements
  1. Java 8 Streams – Filter More Than One Condition

    Simple enough
    resultList.stream() .filter(fixture -> fixture.getHome().equals(team) || fixture.getAway().equals(team))) .collect(toList());
    EDIT: This is on the assumption that order does not matter to you. If your final list needs to have home result and then away, have a look at Elliott Frisch's answer.

  2. Java 8 Streams – Filter More Than One Condition

    Simple enough
    resultList.stream() .filter(fixture -> fixture.getHome().equals(team) || fixture.getAway().equals(team))) .collect(toList());
    EDIT: This is on the assumption that order does not matter to you. If your final list needs to have home result and then away, have a look at Elliott Frisch's answer.

Solution 1

Advertisements

Simple enough

resultList.stream()
        .filter(fixture -> fixture.getHome().equals(team) || fixture.getAway().equals(team)))
        .collect(toList());

EDIT: This is on the assumption that order does not matter to you. If your final list needs to have home result and then away, have a look at Elliott Frisch’s answer.

Original Author user7 Of This Content

Solution 2

Advertisements

If you wan to get fancy with lambdas:

Predicate<FixtureResult> isHome = fr -> fr.getHome().equals(team)
Predicate<FixtureResult> isAway = fr -> fr.getAway().equals(team)

resultList.stream()
  .filter(isHome.or(isAway))
  .collect(toList()));

You could even extract the compose predicate to test it in isolation, with no streams involved, which is good for more complex predicates:

Predicate<FixtureResult> isHomeOrAway = isHome.or(isAway)

assertTrue(isHomeOrAway(homeFixture)); 
...

Original Author Chirlo Of This Content

Solution 3

Advertisements

Assuming the order doesn’t matter, you can do it on one line. Like,

private Collection<FixtureResult> finalResults(Team team) {
    return resultList.stream()
            .filter(fixture -> fixture.getHome().equals(team) 
                    || fixture.getAway().equals(team))
            .collect(toList());
}

If the order matters (home results and then away), you can do it with a single List like

private Collection<FixtureResult> finalResults(Team team) {
    List<FixtureResult> al = new ArrayList<>(resultList.stream()
            .filter(fixture -> fixture.getHome().equals(team)).collect(toList()));
    al.addAll(resultList.stream()
            .filter(fixture -> fixture.getAway().equals(team)).collect(toList()));
    return al;
}

Original Author Elliott Frisch Of This Content

Solution 4

Advertisements

You can simply create a conditions concatenations or can concatenate multiple filter call

Conditions concatenations

myList.stream()
      .filter(element -> (condition1 && condition2 && condition3))

Multiple filter call

myList.stream()
      .filter(element -> condition1)
      .filter(element -> condition2)
      .filter(element -> condition3)

Original Author David Geirola Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment