Java Stream noneMatch() with examples

 251 total views,  3 views today

Welcome to Java 8 series. In this tutorial, you will learn about java stream noneMatch() with examples.

noneMatch() is a part of the Stream pipeline and terminal operation. It belongs to the Stream interface.

  • noneMatch() allows us to identify whether none of the elements of the stream matches the given condition or predicate.
  • noneMatch() returns a boolean.
  • Once noneMatch() is applied to a stream, it returns a boolean result and the stream will be closed.
  • noneMatch() is a terminal operation and hence a stream cannot be reused once this predicate function is applied.
  • noneMatch() returns true, if a stream is empty

Method Description

boolean noneMatch(Predicate<? super T> predicate)

Returns whether no elements of this stream match the provided predicate.

Let us now see a few examples of Java 8 stream noneMatch()

Java stream noneMatch() operation example using lambda

public class StaticReference
{
    public static void main(String[] args)
    {
        List<Integer> list = new ArrayList<>();
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
       
        // Check if the element of a stream is greater than 13
        boolean result = list.stream().noneMatch(i -> i > 13);
        System.out.println("noneMatch found : " + result);
        
        // Check if the element of a stream is greater than 5
        result = list.stream().noneMatch(i -> i > 3);
        System.out.println("noneMatch found : " + result);
    }
}

In the first check, we check if elements are greater than 13. Since none of the element is greater than 13. It returned true.

In the second check, we check if elements are greater than 3. Since the list has elements that are greater than 3. It returned false.

Output

noneMatch found : true
noneMatch found : false

Java stream noneMatch() operation example using Predicate

public class StaticReference
{
    public static void main(String[] args)
    {
        List<Integer> list = new ArrayList<>();
        list.add(3);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        // Check if all elements of a stream is greater than 2
        Predicate<Integer> predicate = (i) -> i > 20;
        boolean result = list.stream().noneMatch(predicate);
        System.out.println("noneMatch found : " + result);
    }
}

Output

noneMatch found : true

Java stream noneMatch() operation example – String

public class StaticReference
{
    public static void main(String[] args)
    {
        List<String> listString = new ArrayList<>();
        listString.add("q");
        listString.add("b");
        listString.add("c");
        listString.add("d");
        // Check if all elements is of length 1
        boolean result = listString
                           .stream()
                           .noneMatch(i -> i.equalsIgnoreCase("f"));
        System.out.println("noneMatch found : " + result);
    }
}

Output

noneMatch found : true

Conclusion

noneMatch() operation is a terminal operation and it is used to identify whether none of the elements of the stream matches the given condition or predicate.

References :Official documentation

Thanks for reading. Please a comment and give us a thumbs up! 

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.