Stream AnyMatch

TheanyMatch function defined in the Stream class is a terminal operation, it takes a Predicate and returns true if any of the elements of the Stream pass the Predicate test else will return false.

package org.wesome.java8;

import java.util.Arrays;
import java.util.List;

class Apple {
    public static void main(String args[]) {
        List<String> apples = Arrays.asList("Macintosh", "Fuji", "Gala", "Jonagold");

        /*-------------------------traditional way-------------------------*/
        boolean anyMatch = false;
        for (String str : apples) {
            if (str.length() >= 4) {
                anyMatch = true;
                break;
            }
        }
        System.out.println("any apple name length greater then 4 = " + anyMatch);

        /*-------------------------anyMatch-------------------------*/
        anyMatch = apples.stream().anyMatch(str -> str.length() > 4);
        System.out.println("any apple name length greater then 4 = " + anyMatch);
    }
}

If the Stream provided to anyMatch function is empty then Predicates will not be evaluated and false will be returned.

package org.wesome.java8;

import java.util.Arrays;
import java.util.List;

class Apple {
    public static void main(String args[]) {
        List<String> apples = Arrays.asList();

        boolean anyMatch = apples.stream().anyMatch(str -> str.length() > 4);
        System.out.println("any apple name length greater then 4 = " + anyMatch);
    }
}

 

The anyMatch function supports multiple Predicatesandornegate function

package org.wesome.java8;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

class Apple {
    public static void main(String args[]) {
        List<String> apples = Arrays.asList("Macintosh", "Fuji", "Gala", "Jonagold");

        boolean anyMatch = apples.stream().anyMatch(apple -> apple.length() < 10 && apple.length() > 8);
        System.out.println("any apple name length less then 10 and greater then 8 = " + anyMatch);

        Predicate<String> nameLength = apple -> apple.length() < 10;
        Predicate<String> namePrice = apple -> apple.length() > 8;
        Predicate<String> combinedCondition = nameLength.and(namePrice);

        System.out.println("*-----------anyMatch with combined Predicate-----------*");
        anyMatch = apples.stream().anyMatch(combinedCondition);
        System.out.println("anyMatch = " + anyMatch);

        System.out.println("*-----------anyMatch with multiple predicate using and-----------*");
        anyMatch = apples.stream().anyMatch(nameLength.and(namePrice));
        System.out.println("anyMatch = " + anyMatch);

        System.out.println("*-----------anyMatch with multiple predicate using or-----------*");
        anyMatch = apples.stream().anyMatch(nameLength.or(namePrice));
        System.out.println("anyMatch = " + anyMatch);

        System.out.println("*-----------anyMatch with multiple predicate using negate-----------*");
        anyMatch = apples.stream().anyMatch(nameLength.negate().and(namePrice));
        System.out.println("anyMatch = " + anyMatch);
    }
}

 

anyMatch on User Defined class

package org.wesome.java8;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

@Data
@AllArgsConstructor
class Fruit {
    int fruitId;
    String fruitName;
    int price;

    public static void main(String[] args) {
        List<Fruit> apples = new ArrayList<>();
        apples.add(new Fruit(1, "Macintosh", 3));
        apples.add(new Fruit(2, "Fuji", 1));
        apples.add(new Fruit(3, "Gala", 4));
        apples.add(new Fruit(4, "Jonagold", 2));

        System.out.println("*-----------anyMatch with and operator-----------*");
        boolean anyMatch = apples.stream().anyMatch(apple -> apple.getFruitName().length() < 10 && apple.getPrice() < 5);
        System.out.println("anyMatch = " + anyMatch);

        Predicate<Fruit> fruitNameLength = apple -> apple.getFruitName().length() < 10;
        Predicate<Fruit> fruitPrice = apple -> apple.getPrice() < 5;
        Predicate<Fruit> combinedCondition = fruitNameLength.and(fruitPrice);

        System.out.println("*-----------anyMatch with combined Predicate-----------*");
        anyMatch = apples.stream().anyMatch(combinedCondition);
        System.out.println("anyMatch = " + anyMatch);

        System.out.println("*-----------anyMatch with multiple predicate using and-----------*");
        anyMatch = apples.stream().anyMatch(fruitNameLength.and(fruitPrice));
        System.out.println("anyMatch = " + anyMatch);

        System.out.println("*-----------anyMatch with multiple predicate using or-----------*");
        anyMatch = apples.stream().anyMatch(fruitNameLength.or(fruitPrice));
        System.out.println("anyMatch = " + anyMatch);

        System.out.println("*-----------anyMatch with multiple predicate using negate-----------*");
        anyMatch = apples.stream().anyMatch(fruitNameLength.negate().and(fruitPrice));
        System.out.println("anyMatch = " + anyMatch);
    }
}

 

follow us on