springjava.com
  • Core Java
  • Spring Core
  • Spring Boot
  • Spring MVC
  • Angular Tutorial

Related Posts

  • Sorting ArrayList of Objects in Java How to Create and Initialize ArrayList in Java List in Java | Interface, Methods, Example Java Synchronized with Examples Generics in Java Java Serialization and Deserialization with Example What are the differences between overloading and overriding What is Overriding in Java What is Method Overloading in Java Java for each loop | Enhanced for loop
Core Java

What is the BiPredicate in Java 8

Last Updated: 16-09-2022 Springjava

Normal Predicate predefined functional interface that accepts only one input and returns a boolean result. But if we provide two inputs to the Predicate, it is not accepted. That’s why Java 8 introduced a BiPredicate predefined functional interface. It accepts two inputs and returns a boolean result. This predefined functional interface is available in the java.util.function package.
Prototype:

interface BiPredicate<T, U>{
public boolean test(T t, U u);

}

Type Parameters:
T - It is the type of the first argument to the predicate
U - It is the type of the second argument the predicate
BiPredicate Example:

import java.util.function.BiPredicate;

public class Test {

public static void main(String[] args) {

BiPredicate<String, Integer> bi = (a, b) -> {

return a.length() == b;

};

boolean result = bi.test("Peter", 5);

System.out.println(result);

boolean result1 = bi.test("Robert", 4);

System.out.println(result1);

boolean result2 = bi.test("William", 7);

System.out.println(result2);
      }
}

Output:

true
false
true

BiPredicate Methods:
    • boolean test(T t, U u): This method evaluates this predicate on the given arguments
    • default BiPredicate or(BiPredicate other): This method returns a composed predicate that represents a short-circuiting logical OR of this predicate and another
    • default BiPredicate and(BiPredicate other):  This method returns a composed predicate that represents a short-circuiting logical AND of this predicate and another
    • default BiPredicate negate():  This method returns a predicate that represents the logical negation of this predicate
BiPredicate and() example:

import java.util.function.BiPredicate;

public class Test {

public static void main(String[] args) {

BiPredicate<Integer, Integer> bi1 = (a, b) -> a%b==0;

BiPredicate<Integer, Integer> bi2 = (a, b) -> a*b<50;

System.out.println("The a is divisible by b and a*b is less than 50: "+bi1.and(bi2).test(10, 2));

System.out.println("The a is divisible by b and a*b is less than 50: "+bi1.and(bi2).test(30, 9));

System.out.println("The a is divisible by b and a*b is less than 50: "+bi1.and(bi2).test(50, 2));
     }
}

Output: 

The a is divisible by b and a*b is less than 50: true
The a is divisible by b and a*b is less than 50: false
The a is divisible by b and a*b is less than 50: false

BiPredicate or() example:

import java.util.function.BiPredicate;

public class Test {

public static void main(String[] args) {

BiPredicate<Integer, Integer> bi1 = (a, b) -> a%b==0;

BiPredicate<Integer, Integer> bi2 = (a, b) -> a*b<50;

System.out.println("The a is divisible by b or a*b is less than 50: "+bi1.or(bi2).test(10, 2));

System.out.println("The a is divisible by b or a*b is less than 50: "+bi1.or(bi2).test(30, 9));

System.out.println("The a is divisible by b or a*b is less than 50: "+bi1.or(bi2).test(50, 2));
   }
}

Output: 

The a is divisible by b or a*b is less than 50: true
The a is divisible by b or a*b is less than 50: false
The a is divisible by b or a*b is less than 50: true

BiPredicate negate() example:

import java.util.function.BiPredicate;

public class Test {

public static void main(String[] args) {

BiPredicate<Integer, Integer> bi1 = (a, b) -> a%b==0;

BiPredicate<Integer, Integer> bi2 = (a, b) -> a*b<50;

System.out.println("The a is not divisible by b or a*b is not less than 50: "+bi1.negate().test(10, 2));

System.out.println("The a is not divisible by b or a*b is not less than 50: "+bi2.negate().test(30, 9));

System.out.println("The a is not divisible by b or a*b is not less than 50: "+bi1.negate().test(50, 2));
    }
}

Output: 

The a is not divisible by b or a*b is not less than 50: false
The a is not divisible by b or a*b is not less than 50: true
The a is not divisible by b or a*b is not less than 50: false

Conclusion:
This topic is explained What is a BiPredicate? What are the methods of it? How to use it?

What is a Java Bean Class? Java 8 Features Write Less Lines Of Code Lambda Expression In Java 8 Features

Leave your thought here

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

springjava_com
  • springjava.com
Learn
    • Core Java
    • Spring Core
    • Spring Boot
    • Spring MVC
    • Angular Tutorial
Quick links
  • About Us
  • Contact Us
  • Privacy Policy
© 2021 Spring Java. All Rights Reserved.
springjava.com
  • Core Java
  • Spring Core
  • Spring Boot
  • Spring MVC
  • Angular Tutorial
  • About Us
  • Contact Us