BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java 8 Stream – noneMatch() example

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In the last tutorial we discussed java stream anyMatch() method. The stream noneMatch() method works just opposite to the anyMatch() method, it returns true if none of the stream elements match the given predicate, it returns false if any of the stream elements matches the condition specified by the predicate.

Java Stream noneMatch() example

import java.util.List;
import java.util.function.Predicate;
import java.util.ArrayList;
class Student{
   int stuId; 
   int stuAge;  
   String stuName; 
   Student(int id, int age, String name){
      this.stuId = id;   
      this.stuAge = age;  
      this.stuName = name;  
   }  
   public int getStuId() {  
      return stuId;  
   }
   public int getStuAge() {
      return stuAge;
   }
   public String getStuName() {
      return stuName;
   }
   public static List<Student> getStudents(){ 
      List<Student> list = new ArrayList<>(); 
      list.add(new Student(11, 28, "Lucy")); 
      list.add(new Student(28, 27, "Kiku")); 
      list.add(new Student(32, 30, "Dani")); 
      list.add(new Student(49, 27, "Steve")); 
      return list; 
   }
}
public class Example {
    public static void main(String[] args) {
       Predicate<Student> p1 = s -> s.stuName.startsWith("L"); 
       Predicate<Student> p2 = s -> s.stuAge < 28 && s.stuName.startsWith("P"); 
       List<Student> list = Student.getStudents();

       /* noneMatch() method returns true if none of the stream elements matches
        * the given predicate
        */
       /* This will return false because there is a element in the stream that
        * matches the condition specified in the predicate p1. 
        * Condition: Student Name should start with letter "L"
        * Stream element matched: Lucy 
        */
       boolean b1 = list.stream().noneMatch(p1); 
       System.out.println("list.stream().noneMatch(p1): "+b1); 
 
       /* This will return true because none of the elements  
        * matches the condition specified in the predicate p2.  
        * Condition: Student Name should start with letter "P" and age must be <28 
        * Stream element matched: none 
        */
       boolean b2 = list.stream().noneMatch(p2); 
       System.out.println("list.stream().noneMatch(p2): "+b2); 
   }
}

Output:

list.stream().noneMatch(p1): false
list.stream().noneMatch(p2): true

The first call to the noneMatch() method with predicate p1 as argument returns false because there is an element in the stream that matches the condition specified by p1.
The second call to the noneMatch() method with predicate p2 as argument returns true because there is no element in the stream that matches the given conditions.

Related Posts:

  1. Java 8 Stream
  2. Java 8 Stream Filter
  3. Java Stream filter null values example
  4. Java 8 Stream Collectors Class

Top Related Articles:

  1. Java 9 – @SafeVarargs Annotation (with examples)
  2. Java 8 – Adding Days to the LocalDate
  3. Java String valueOf() method
  4. Java StringBuilder charAt()
  5. Java 8 Interface Changes – default method and static method

Tags: Java8-Features

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap