Predicate系列函数接口表示接收一个任意类型的参数,但是只会返回boolean类型的结果值。
-
Predicate
源码如下,可以看到该函数接口中有5个方法,其中 一个是静态方法。@FunctionalInterface public interface Predicate<T> { /** * 计算给定的参数 */ boolean test(T t); /** * 且的关系,相当于逻辑运算中 && 的作用。计算两个Predicate对象结果是否都为true 。 */ default Predicate<T> and(Predicate<? super T> other) { Objects.requireNonNull(other); return (t) -> test(t) && other.test(t); } /** * 取反,相当于 ! 的作用。返回给定参数计算后的值再取反。 */ default Predicate<T> negate() { return (t) -> !test(t); } /** * 或的关系,相当于逻辑运算中 || 的作用,计算两个Predicate对象执行后的值,有一个为true结果便为true 。 */ default Predicate<T> or(Predicate<? super T> other) { Objects.requireNonNull(other); return (t) -> test(t) || other.test(t); } /** * 注意:这个是静态方法,其实就是Object类中的equal()方法 */ static <T> Predicate<T> isEqual(Object targetRef) { return (null == targetRef) ? Objects::isNull : object -> targetRef.equals(object); } }
示例代码如下:
public class TestPredicate { public static void main(String[] args) { test(100, val -> val > 50); // 结果:true testNegate(100, v -> v > 50); // 结果:false testOr(100, v -> v > 200, o -> o > 300); // 结果:false testOr(100, v -> v > 200, o -> o > 50); // 结果:true testOr(100, v -> v > 50, o -> o > 300); // 结果:true testAnd("test", str -> str.equals("test"), o -> o.length() == 4); // 结果:true testAnd("test", str -> str.equals("test"), o -> o.length() == 8); // 结果:false testAnd("test", str -> str.equals("testtest"), o -> o.length() == 4); // 结果:false testEqual("test", "test"); // 结果:true testEqual("test", "testtest"); // 结果:false testEqual("testtest", "test"); // 结果:false } public static void test(int val, Predicate<Integer> predicate) { boolean test = predicate.test(val); System.out.println(test); } public static void testNegate(int val, Predicate<Integer> predicate) { Predicate<Integer> negate = predicate.negate(); boolean test = negate.test(val); System.out.println(test); } public static void testOr(int val, Predicate<Integer> predicate, Predicate<Integer> other) { boolean test = predicate.or(other).test(val); System.out.println(test); } public static void testAnd(String str, Predicate<String> predicate, Predicate<String> other) { boolean test = predicate.and(other).test(str); System.out.println(test); } public static void testEqual(String s1, String s2) { Predicate<Object> equal = Predicate.isEqual(s1); boolean test = equal.test(s2); System.out.println(test); } }
-
BiPredicate
Bipredicate是接收两个参数的接口,和Predicate相比少了一个静态方法isEqual()。其余用法都一样。看一下源码@FunctionalInterface public interface BiPredicate<T, U> { boolean test(T t, U u); default BiPredicate<T, U> and(BiPredicate<? super T, ? super U> other) { Objects.requireNonNull(other); return (T t, U u) -> test(t, u) && other.test(t, u); } default BiPredicate<T, U> negate() { return (T t, U u) -> !test(t, u); } default BiPredicate<T, U> or(BiPredicate<? super T, ? super U> other) { Objects.requireNonNull(other); return (T t, U u) -> test(t, u) || other.test(t, u); } }
其实还是那四个方法,具体使用如下:
public class TestBiPredicate { public static void main(String[] args) { test(100, 200, (v1, v2) -> v1 > v2); // 结果:false testOr("test", "testtest", (s1, s2) -> s2.length() == s1.length(), (o1, o2) -> o1.equals(o2)); // 结果:false testOr("test", "testtest", (s1, s2) -> s2.length() == 4, (o1, o2) -> "testtest".equals(o2)); // 结果:true testOr("test", "testtest", (s1, s2) -> s2.length() == 4, (o1, o2) -> "test".equals(o2)); // 结果:false testOr("test", "testtest", (s1, s2) -> s2.length() == 8, (o1, o2) -> "testtest".equals(o2)); // 结果:true testAnd("test", "test", (s1, s2) -> s2.length() == s2.length(), (o1, o2) -> o1.equals(o2)); // 结果:true } public static void test(int v1, int v2, BiPredicate<Integer, Integer> predicate) { boolean test = predicate.test(v1, v2); System.out.println(test); } public static void testOr(String s1, String s2, BiPredicate<String, String> predicate, BiPredicate<String, String> other) { BiPredicate<String, String> or = predicate.or(other); boolean test = or.test(s1, s2); System.out.println(test); } public static void testAnd(String s1, String s2, BiPredicate<String, String> predicate, BiPredicate<String, String> other) { BiPredicate<String, String> and = predicate.and(other); boolean test = and.test(s1, s2); System.out.println(test); } }
-
IntPredicate
IntPredicate也是有四个方法,test() 、 or()、 and()、 negate(),只处理给定的一个Int类型的参数,并返回boolean类型的值,就补举例了,参考Predicate的用法。 -
LongPredicate
同IntPredicate一样,包含四个方法,只接受Long类型的参数,参考Predicate的用法。 -
DoublePredicate
同IntPredicate一样,包含四个方法,只接受Double类型的参数,参考Predicate的用法。