Java 8 – Stream noneMatch() method with examples

In this article, we will discuss Stream’s noneMatch() method in details with examples

Read below methods which are similar to noneMatch() method

1. Stream noneMatch() method :

  • This Stream method is a terminal operation and returns boolean value
  • noneMatch() method returns true if none of the elements present in the original Stream satisfies the provided Predicate condition,
  • noneMatch() method returns false even if at least one element of the original Stream matches/satisfies the given Predicate
  • Stream’s noneMatch() method is stateless which means it is non-interfering with other elements in the stream
  • May not evaluate the predicate on all elements if not necessary for determining the result
  • It is a short-circuiting terminal operation, as Stream pipeline ends if there is one match for the provided Predicate
  • If the stream is empty then true is returned and the predicate is not evaluated
  • noneMatch() method is just opposite of the anyMatch() method
  • Method signature :- boolean noneMatch(Predicate<? super T> predicate)



2. Stream noneMatch() method examples :

2.1 To find specific element NOT present

  • First list contains integer numbers
  • We are searching for number greater than 10 using noneMatch() method and it returns true because all numbers in this list are less than or equal to 10
  • Another Predicate condition in the integer list is to search for positive numbers using noneMatch() method and this time it returns false because all numbers in this list are positive numbers
  • Second list contains String elements
  • We are searching for element named ‘Titan‘ using noneMatch() method and it returns true because none of the elements in this String list is matching
  • Similarly, we are searching for another String element named ‘Power‘ using noneMatch() method and it returns false because there is one element present
package net.bench.resources.stream.nonematch.example;

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

public class StreamNoneMatchMethod {

	public static void main(String[] args) {

		// 1. list of 10 natural numbers
		List<Integer> numbers = Arrays
				.asList(1,2,3,4,5,6,7,8,9,10);

		// 1.1 predicate condition for noneMatch
		boolean bool1 = numbers
				.stream()
				.noneMatch(num -> num > 10);

		// 1.2 print to console
		System.out.println("Original list of integers : " + numbers);
		System.out.println("\nnoneMatch(number greater than 10) = " 
				+ bool1);

		// 1.3 in line Predicate condition for noneMatch
		System.out.println("noneMatch(positive numbers)  = "  
				+ numbers.stream().noneMatch(num -> num > 0));

		// 2. list of 5 Strings
		List<String> sectors = Arrays.asList(
				"Motor", 
				"Power", 
				"Steel", 
				"Chemicals", 
				"Capital"
				);

		// 2.1 noneMatch - predicate to find String element not present
		boolean bool2 = sectors
				.stream()
				.noneMatch(str -> str.contains("Titan"));

		// 2.2 print to console
		System.out.println("\n\nOriginal list of Strings : " + sectors);
		System.out.println("\nnoneMatch('Titan') = " + bool2);

		// 2.3 in line Predicate condition for noneMatch
		System.out.println("noneMatch('Power') = "  
				+ sectors.stream().noneMatch(str -> str.contains("Power")));
	}
}

Output:

Original list of integers : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

noneMatch(number greater than 10) = true
noneMatch(positive numbers)  = false


Original list of Strings : [Motor, Power, Steel, Chemicals, Capital]

noneMatch('Titan') = true
noneMatch('Power') = false

2.2 Multiple Predicates for noneMatch() method

  • A Student POJO is defined with 3 attributes
  • A list contains 5 Student objects defining their attributes like rollNumber, name, and age
  • We have defined 2 simple Predicate namely p1 and p2 and one more joined Predicate which is the combined Predicate of p1 && p2
  • 1st Predicate – find Student whose name starts with ‘N‘ using noneMatch() method which returns true, as there are no Students whose name starts with N
  • 2nd Predicate – find Student with Age greater than 24 using noneMatch() method which returns true, as there are no Students whose age is greater than 24
  • 3rd Predicate – Student whose name starts with ‘N‘ and their Age is greater than 24 using noneMatch() method which returns true, as there are no Students whose name starts with N and their Age is greater than 24
  • inline Predicate – Student whose name starts with ‘V‘ and their Age is less than 18 using noneMatch() method which returns false, as there is one Student whose name starts with V and his Age is less than 18

Student.java

package net.bench.resources.stream.anymatch.example;

public class Student {

	// member variables
	private int rollNumber;
	private String name;
	private int age;

	// constructors
	public Student(int rollId, String name, int age) {
		super();
		this.rollNumber = rollId;
		this.name = name;
		this.age = age;
	}

	// getters & setters
	public int getRollId() {
		return rollNumber;
	}
	public void setRollId(int rollId) {
		this.rollNumber = rollId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}

	// toString()
	@Override
	public String toString() {
		return "Student [rollNumber=" + rollNumber 
				+ ", name=" + name 
				+ ", age=" + age 
				+ "]";
	}
}

StreamAnyMatchMethodForStudents.java

package net.bench.resources.stream.nonematch.example;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

import net.bench.resources.stream.min.max.example.Student;

public class StreamNoneMatchMethodForStudents {

	public static void main(String[] args) {

		// list of students
		List<Student> students = Arrays.asList(
				new Student(1, "Viraj", 17),
				new Student(2, "Krishnanand", 18),
				new Student(3, "Rishi", 16),
				new Student(4, "Suresh", 23),
				new Student(5, "Aditya", 21)
				);

		// predicate 1 - name starts with N
		Predicate<Student> p1 = stud -> stud.getName().startsWith("N");

		// predicate 2 - age older than 24
		Predicate<Student> p2 = stud -> stud.getAge() > 24;

		// combined predicate p3 = p1 && p2
		Predicate<Student> p3 = p1.and(p2);

		// print all Students to console
		System.out.println("List of Students :-");
		students.stream().forEach(student -> System.out.println(student));

		// 1. noneMatch(Student name starts with 'N')
		boolean boolP1 = students.stream().noneMatch(p1);
		System.out.println("\n1. noneMatch(Student name starts with 'N') = " 
				+ boolP1);

		// 2. noneMatch(Student Age greater than 24)
		boolean boolP2 = students.stream().noneMatch(p2);
		System.out.println("\n2. noneMatch(Student Age greater than 24) = "
				+ boolP2);

		// 3. noneMatch(Student Name starts with 'N' && Age greater than 24)
		boolean boolP3 = students.stream().noneMatch(p3);
		System.out.println("\n3. noneMatch(Student Name "
				+ "starts with 'N' && Age greater than 24) = "
				+ boolP3);

		// 4. in line multiple Predicate condition
		boolean boolP4 = students.stream().noneMatch(
				stud -> (stud.getName().startsWith("V") 
						&& stud.getAge() < 18));
		System.out.println("\n4. noneMatch(Student Name "
				+ "starts with 'V' && Age less than 18) = " 
				+ boolP4);
	}
}

Output:

List of Students :-
Student [rollNumber=1, name=Viraj, age=17]
Student [rollNumber=2, name=Krishnanand, age=18]
Student [rollNumber=3, name=Rishi, age=16]
Student [rollNumber=4, name=Suresh, age=23]
Student [rollNumber=5, name=Aditya, age=21]

1. noneMatch(Student name starts with 'N') = true

2. noneMatch(Student Age greater than 24) = true

3. noneMatch(Student Name starts with 'N' && Age greater than 24) = true

4. noneMatch(Student Name starts with 'V' && Age less than 18) = false

References:

Happy Coding !!
Happy Learning !!

Java 8 - Stream allMatch() method with examples
Java 8 - Stream anyMatch() method with examples