这篇文章属于 Java 8 教程(LTS)系列教程,点击阅读更多相关文章。

BiPredicatePredicate 函数接口一样,都是返回一个布尔类型,唯一不同的是 Predicate 接受一个参数,而 BiPredicate 可以接受两个不同类型的参数。

BiPredicate 在 Java 8 中源码:

package java.util.function;

import java.util.Objects;
@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);
    }
}

1. BiPredicate <T, U>

示例:判断字符串长度是否是指定长度。

package com.wdbyte;

import java.util.function.BiPredicate;

public class Java8FunctionBiPredicate {

    public static void main(String[] args) {
        // 判断字符串的长度是否是指定长度
        BiPredicate<String, Integer> biFunction = (s, i) -> s.length() == i;
        System.out.println(biFunction.test("java", 3));
        System.out.println(biFunction.test("java", 4));
        System.out.println(biFunction.test("www.wdbyte.com", 10));
        System.out.println(biFunction.test("www.wdbyte.com", 14));
    }
}

输出结果:

false
true
false
true

2. BiPredicate And

示例:判断字符串是否以指定字符开头且以指定字符结束。

BiPredicate<String, String> startPredicate = (s1, s2) -> s1.startsWith(s2);
BiPredicate<String, String> endPredicate = (s1, s2) -> s1.endsWith(s2);

boolean test = startPredicate.and(endPredicate).test("wdbyte", "w");
System.out.println(test);
boolean test1 = startPredicate.and(endPredicate).test("wsw", "w");
System.out.println(test1);

输出结果:

false
true

3. BiPredicate 作为参数

示例:使用 BiPredicate 作为参数,实现对 List 集合的过滤操作。

package com.wdbyte;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;

public class Java8FunctionBiPredicateParams {

    public static void main(String[] args) {
        List<Dog> list = new ArrayList<>();
        list.add(new Dog("牧羊犬", 1));
        list.add(new Dog("牧羊犬", 2));
        list.add(new Dog("哈士奇", 2));
        list.add(new Dog("田园犬", 3));
        // 筛选2岁的狗
        BiPredicate<String, Integer> age = (n, a) -> a == 2;
        // 筛选牧羊犬
        BiPredicate<String, Integer> name = (n, a) -> "牧羊犬".equals(n);
        // 筛选2岁的狗或者筛选牧羊犬
        BiPredicate<String, Integer> ageAndName = (n, a) -> "牧羊犬".equals(n) || a == 2;
        System.out.println(filter(list, age));
        System.out.println(filter(list, name));
        System.out.println(filter(list, ageAndName));

    }

    public static <T extends Dog> List<T> filter(List<T> list, BiPredicate<String, Integer> biPredicate) {
        return list.stream()
            .filter(dog -> biPredicate.test(dog.getName(), dog.getAge()))
            .collect(Collectors.toList());
    }
}

Dog.java

public class Dog {

    private String name;
    private Integer age;

    public Dog() {
    }

    public Dog(String name) {
        this.name = name;
    }

    public Dog(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    // ge,set,toString ...
}

输出结果:

[Dog{, name='牧羊犬', age=2}, Dog{, name='哈士奇', age=2}]
[Dog{, name='牧羊犬', age=1}, Dog{, name='牧羊犬', age=2}]
[Dog{, name='牧羊犬', age=1}, Dog{, name='牧羊犬', age=2}, Dog{, name='哈士奇', age=2}]

参考

- BiPredicate (Java Platform SE 8 )

- Java 8 Predicate 函数接口