Java 8 Functional Interfaces Java Java API
Interface:
java.util.function.BiPredicate<T,U>
Method:
boolean test(T t, U u)
Evaluates this predicate on the given arguments.
- Parameters:
-
t
- the first input argument
-
u
- the second input argument
- Returns:
-
true
if the input arguments match the predicate, otherwise
false
Examples
 package com.logicbig.example.bipredicate;
import java.util.function.BiPredicate;
public class TestExample {
public static void main(String... args) { BiPredicate<Integer, Integer> multiplePredicate = (a, b) -> a % b == 0; boolean b = multiplePredicate.test(72, 9); System.out.println(b); } }
Outputtrue
Assigning to a static method reference  package com.logicbig.example.bipredicate;
import java.util.function.BiPredicate;
public class TestExample2 {
public static void main(String... args) { BiPredicate<Integer, Integer> multiplePredicate = TestExample2::isMultipleOf; boolean b = multiplePredicate.test(7847, 1121); System.out.println(b); }
private static boolean isMultipleOf(Integer a, Integer b) { return a % b == 0; } }
Outputtrue
 package com.logicbig.example.bipredicate;
import java.util.List; import java.util.function.BiPredicate; import java.util.stream.Collectors;
public class TestExample3 {
public static void main(String... args) { List<Integer> list = List.of(1, 4, 5, 8, 11); //static method reference List<Integer> l = searchList(list, Integer::equals, 4); System.out.println(l);
//instance method reference List<String> cars = List.of("cadillac", "chrysler", "ferrari"); List<String> l2 = searchList(cars, String::startsWith, "c"); System.out.println(l2);
//instance method reference on a given instance String s = "I drive a ferrari car"; List<String> l3 = searchList(cars, s::startsWith, 10); System.out.println(l3); }
public static <T, U> List<T> searchList(List<T> l, BiPredicate<T, U> searchPredicate, U userObject) { return l.stream().filter(e -> searchPredicate.test(e, userObject)) .collect(Collectors.toList()); }
}
Output[4] [cadillac, chrysler] [ferrari]
|
|