default Predicate<T> and(Predicate<? super T> other)
The and() from the Java 8 Predicate Functional Interface is a default function that returns a composed predicate that represents a short-circuiting logical AND of this predicate and another, i.e. if while evaluating a composed predicate any predicate outcome is false, then the other predicate is not evaluated.
Let's take a look at an example.
We create a Lambda function with a predicate and() that the Person should be,
- a Male, AND
- from USA, AND
- age > 18
package org.code2care.examples;
import java.util.function.Predicate;
class Person {
private String name;
private int age;
private String gender;
private String nationality;
public Person(String name, int age, String gender, String nationality) {
this.name = name;
this.age = age;
this.gender = gender;
this.nationality = nationality;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public String getNationality() {
return nationality;
}
}
public class PredicateAndExample {
public static void main(String... args) {
Predicate<Person> isMale = person -> "Male".equalsIgnoreCase(person.getGender());
Predicate<Person> isAdult = person -> person.getAge() > 18;
Predicate<Person> isFromUSA = person -> "USA".equalsIgnoreCase(person.getNationality());
Predicate<Person> isEligible = isMale.and(isAdult).and(isFromUSA);
Person person1 = new Person("Andrew", 25, "Male", "USA");
Person person2 = new Person("Ela", 17, "Female", "Canada");
Person person3 = new Person("David", 29, "Male", "England");
System.out.println("Person 1 Eligibility: " + isEligible.test(person1));
System.out.println("Person 2 Eligibility: " + isEligible.test(person2));
System.out.println("Person 3 Eligibility: " + isEligible.test(person3));
}
}
Output:
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- [Eclipse] Locate Preferences in macOS - MacOS
- How to Get the Relative Path of a file in Python Program - Python
- Pdf Text to Speech option in Mac OS X Preview App - Mac-OS-X
- Setting Java_Home Environment variable on Windows Operating System - Java
- pip/pip3 ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied - PIP
- Fix Microsoft Store Error Code: 0x80072EFD - Microsoft
- JavaScript: Check if variable is a number - JavaScript
- Fix: type argument is not within bounds of type-variable T - Java