Stream AllMatch

The allMatch function defined in the Stream class takes a Predicate and makes sure all the elements of the Stream pass the Predicate test.

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 allMatch = true;
        for (String str : apples) {
            if (str.length() >= 10) {
                allMatch = false;
                break;
            }
        }
        System.out.println("any apple name length greater then 10 = " + allMatch);

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

If the Stream provided to allMatch function is empty then Predicates will not be evaluated and true 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 allMatch = apples.stream().allMatch(str -> str.length() < 10);

        System.out.println("any apple name length greater then 10 = " + allMatch);
    }
}

allMatch function supports multiple Predicates, and, or, negate function

package org.wesome.java8;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.Arrays;
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<String> apples = Arrays.asList("Macintosh", "Fuji", "Gala", "Jonagold");

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

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

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

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

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

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

allMatch 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("*-----------allMatch with and operator-----------*");
        boolean allMatch = apples.stream().allMatch(apple -> apple.getFruitName().length() < 10 && apple.getPrice() < 5);
        System.out.println("allMatch = " + allMatch);

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

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

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

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

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

 

follow us on