In this post, we are going to see the Java 8 Stream allMatch() method and how it works with an example.
Stream allMatch() method is a short-circuiting terminal operation that will return true if all elements of the Stream satisfy the given Predicate
Stream allMatch() Syntax
boolean java.util.stream.Stream.allMatch(Predicate predicate)
Stream.allMatch() will return true only if all elements of the Stream satisfy the given Predicate otherwise it will return false
If the stream is empty then true is returned and the predicate is not evaluated.
allMatch() looks for all elements in the stream that will match with the given predicate and if one of the elements does not satisfy the given predicate allMatch method may skip evaluating the remaining elements earlier and will return false. This process is called short-circuit evaluation and hence
it is called a short-circuiting terminal operation.
If the stream is empty then it returns true always and stops evaluating the predicate.
Stream allMatch() Example
Candidate.java
package com.digitizedpost.match;
public class Candidate {
private String name;
private String qualification;
private String jobType;
private boolean previouslyAttended;
private int score;
public Candidate(String name, String qualification, String jobType,
boolean previouslyAttended, int score) {
this.name = name;
this.qualification = qualification;
this.jobType = jobType;
this.previouslyAttended = previouslyAttended;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getQualification() {
return qualification;
}
public void setQualification(String qualification) {
this.qualification = qualification;
}
public String getJobType() {
return jobType;
}
public void setJobType(String jobType) {
this.jobType = jobType;
}
public boolean isPreviouslyAttended() {
return previouslyAttended;
}
public void setPreviouslyAttended(boolean previouslyAttended) {
this.previouslyAttended = previouslyAttended;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
MatchingCandidate.java
package com.digitizedpost.match;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class MatchingCandidate {
public List<Candidate> getJobCandidates() {
List<Candidate> list = Arrays.asList(
new Candidate("John", "Degree", "Banking", false, 70),
new Candidate("James", "Degree", "Banking", false, 72),
new Candidate("Harry", "Degree", "Banking", true, 79),
new Candidate("Justin", "Degree", "Banking", false, 95),
new Candidate("Jennifer", "Degree", "Banking", false, 87));
return list;
}
public static void main(String... args) {
MatchingCandidate m = new MatchingCandidate();
boolean candidatesScoreMatch = m.getJobCandidates().stream()
.allMatch(c -> c.getScore() >= 70);
System.out.println("Candidates Pass Score greater than 70 ? "
+ candidatesScoreMatch);
boolean candidatesQualifMatch = m.getJobCandidates().stream()
.allMatch(c -> c.getQualification().equals("Degree")
&& c.getJobType().equals("Banking"));
System.out.println("All Candidates has Degree and Applied for Banking ? "
+ candidatesQualifMatch);
//one candidate already attended interview
boolean candidatesPrevAttendMatch = m.getJobCandidates().stream()
.allMatch(c -> c.isPreviouslyAttended() != true);
System.out.println("All Candidates previously not attended ? "
+ candidatesPrevAttendMatch);
//allMatch() return true for an empty stream
boolean match = Stream.empty().allMatch(e -> false);
System.out.println(match);
}
}
candidatesScoreMatch will set to true only when all the candidate's pass score is greater than or equal to 70.
candidatesPrevAttendMatch returns false because one candidate already attended the interview and that one element doesn't match the given predicate.
Result of the above program
Candidates Pass Score greater than 70 ? true
All Candidates has Degree and Applied for Banking ? true
All Candidates previously not attended ? false
true
Later in this series, we will discuss the anyMatch() and noneMatch() Stream methods.