Stream NonMatch

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

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

If the Stream provided to noneMatch 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 noneMatch= apples.stream().noneMatch(str -> str.length() < 10);

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

 

noneMatch function supports multiple Predicatesandornegate 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("*-----------noneMatch with and operator-----------*");
        boolean noneMatch = apples.stream().noneMatch(apple -> apple.length() < 10 && apple.length() > 2);
        System.out.println("noneMatch = " + noneMatch);

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

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

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

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

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

 

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

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

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

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

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

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

 

follow us on