Stream find and match

In this example we show java.util.Stream examples on finding whether some elements match a given property in a set of data through the allMatch, anyMatch, nonMatch, findFirst and findAny methods.

Setup

class HiddenObjectGame {

    String name = null;
    double rating;
    double totalPlays;
    LocalDate releaseDate;

    public HiddenObjectGame(String name, double rating, double totalPlays,
            LocalDate releaseDate) {
        super();
        this.name = name;
        this.rating = rating;
        this.totalPlays = totalPlays;
        this.releaseDate = releaseDate;
    }

    //...
}

List<HiddenObjectGame> games;

@Before
public void setUp() {

    games = new ArrayList<>();

    games.add(new HiddenObjectGame("The Daily SNOOP", 4.65, 22207,
            LocalDate.of(2011, Month.MARCH, 15)));
    games.add(new HiddenObjectGame("Gardenscapes", 4.43, 1670, LocalDate
            .of(2009, Month.DECEMBER, 29)));
    games.add(new HiddenObjectGame("Barn Yarn", 4.55, 1453, LocalDate.of(
            2013, Month.JUNE, 18)));
    games.add(new HiddenObjectGame("The Treasures of Mystery Island", 4.09,
            594, LocalDate.of(2009, Month.JUNE, 29)));
    games.add(new HiddenObjectGame("Paparazzi", 3.74, 391, LocalDate.of(
            2006, Month.OCTOBER, 10)));
}

allMatch

The Stream.allMatch method returns whether all elements of a stream match the provided predicate. If the stream is empty then true is returned and the predicate is not evaluated. For example, you can use it to find if all the games name contains a letter 'a'.

@Test
public void stream_allMatch() {

    boolean containVowel = games.stream().allMatch(
            game -> game.getName().contains("a"));

    assertTrue(containVowel);

}

anyMatch

The Stream.anyMatch method can be used to determine if any element in the stream matches a supplied predicate. If the stream is empty then false is returned and the predicate is not evaluated. For example, you can check if any game total plays is greater than 1000.

@Test
public void stream_anyMatch() {

    boolean playsGt1000 = games.stream().anyMatch(
            game -> game.getTotalPlays() > 1000);

    assertTrue(playsGt1000);
}

noneMatch

The opposite of Stream.allMatch, Stream.noneMatch will check whether no elements of a given stream match the provided predicate. If the stream is empty then true is returned and the predicate is not evaluated. In the example below, we will check if any game matches a rating of above 5 and if the total plays is greater than 100.

@Test
public void stream_noneMatch() {

    Predicate<HiddenObjectGame> playsGt1000 = p -> p.getTotalPlays() > 1000;
    Predicate<HiddenObjectGame> ratingGt5 = p -> p.getRating() > 5;

    boolean noneMatch = games.stream()
            .noneMatch(ratingGt5.and(playsGt1000));

    assertTrue(noneMatch);
}

findFirst

Similar to guava's getFirst method, Stream.findFirst will return the first element of a given stream or will return an empty Optional if the stream is empty. If the stream has no encounter order, then any element may be returned. In this example, we will simply call findFirst which returns the first element in the games collection.

@Test
public void stream_findFirst() {

    Optional<HiddenObjectGame> firstHiddenGame = games.stream().findFirst();

    assertTrue(firstHiddenGame.isPresent());
    assertEquals("The Daily SNOOP", firstHiddenGame.get().getName());
}

findAny

Stream.findAny will return Optional describing some element in the stream or empty Optional if the stream is empty. In this example, we want to find any elements in the games collection that has a release date of April which there is none.

@Test
public void stream_findAny() {

    Predicate<HiddenObjectGame> releaseDateInApril = p -> Month.APRIL == p
            .getReleaseDate().getMonth();

    Optional<HiddenObjectGame> hiddenGameReleaseInApril = games.stream()
            .filter(releaseDateInApril).findAny();
    
    assertFalse(hiddenGameReleaseInApril.isPresent());
}