Monday, January 24, 2022

Java Stream - noneMatch() With Examples

In JavaStream API there is a method allMatch() which is used to check if all the elements in the Stream match the given condition. There is also a noneMatch() method in Java Stream API to check whether no elements of this stream match the provided predicate.

Syntax of noneMatch() method

boolean noneMatch(Predicate<? super T> predicate)

Here predicate is a parameter of type Predicate functional interface that provides the condition to be applied to elements of this stream.

Method returns true if no elements of the stream match the provided predicate, otherwise false. If Stream is empty then also true is returned.

noneMatch is a short-circuiting terminal operation. It’s a terminal operation means the stream pipeline is considered consumed after allMatch() operation, and can no longer be used. It is also short-circuiting which means when presented with infinite input, it may terminate in finite time.

noneMatch() Java examples

1. In this example we’ll use noneMatch() to quickly verify if all the elements in the List are less than 50. If we use noneMatch with the conditon to check if any element is greater than 50 then it should return true for our requirement (all elements less than 50).

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

public class NoneMatchStream {
  public static void main(String[] args) {
    List<Integer> numList = Arrays.asList(30, 40, 22, 45, 27, 49, 47);
      boolean result = numList.stream().noneMatch(n -> n > 50);
      System.out.println(result);
  }
}

Output

true
2. In this example we’ll have a Stream of Employee objects where we want to check if none of the employee has salary less than 8000.

Employee class

public class Employee {
  private String empId;
  private int age;
  private String name;
  private char gender;
  private int salary;
  Employee(String empId, int age, String name, char gender, int salary){
    this.empId = empId;
    this.age = age;
    this.name = name;
    this.gender = gender;
    this.salary = salary;
  }
  public String getEmpId() {
    return empId;
  }

  public int getAge() {
    return age;
  }

  public String getName() {
    return name;
  }

  public char getGender() {
    return gender;
  }

  public int getSalary() {
    return salary;
  }
  @Override
  public String toString() {
    return "Emp Id: " +  getEmpId() + " Name: " + getName() + " Age: " + getAge();
  }
}
public class NoneMatchStream {
  public static void main(String[] args) {
    List<Employee> empList = Arrays.asList(new Employee("E001", 40, "Ram", 'M', 5000), 
                new Employee("E002", 35, "Shelly", 'F', 7000), 
                new Employee("E003", 24, "Mark", 'M', 9000), 
                new Employee("E004", 37, "Ritu", 'F', 11000),
                new Employee("E005", 32, "Anuj", 'M', 12000), 
        new Employee("E006", 28, "Amy", 'F', 14000)); 
    boolean result = empList.stream().noneMatch(e -> e.getSalary() < 8000);
    System.out.println(result);
  }
}

Output

false

Result is false as not there are employees with salary less than 8000.

That's all for this topic Java Stream - noneMatch() With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Advanced Tutorial Page


Related Topics

  1. Java Stream - anyMatch() With Examples
  2. Java Stream - Convert Stream to Array
  3. Java Stream - findAny() With Examples
  4. Java Stream API Interview Questions And Answers

You may also like-

  1. Just In Time Compiler (JIT) in Java
  2. How to Loop Through a Map in Java
  3. break Statement in Java With Examples
  4. matches() method in Java String
  5. How to Reverse a Linked List in Java
  6. Spring MVC Dropdown Example Using Select, Option And Options Tag
  7. Spring NamedParameterJdbcTemplate Select Query Example
  8. Using Combiner in Hadoop MapReduce to Improve Performance

No comments:

Post a Comment