Java8 BiPredicate的使用(Predicate使用2个参数)

在Java 8中, BiPredicate是一个函数接口,它接受两个参数并返回一个布尔值,基本上,此BiPredicatePredicate相同,相反,它需要2个参数进行测试。

  1.  
    @FunctionalInterface
  2.  
    public interface BiPredicate<T, U> {
  3.  
    boolean test(T t, U u);
  4.  
    }

进一步阅读
Java 8谓词示例

1. BiPredicate Hello World。

如果字符串长度匹配提供的长度?

JavaBiPredicate1.java
  1.  
    package com.mkyong.java8;
  2.  
     
  3.  
    import java.util.function.BiPredicate;
  4.  
     
  5.  
    public class JavaBiPredicate1 {
  6.  
     
  7.  
    public static void main(String[] args) {
  8.  
     
  9.  
    BiPredicate<String, Integer> filter = (x, y) -> {
  10.  
    return x.length() == y;
  11.  
    };
  12.  
     
  13.  
    boolean result = filter.test("mkyong", 6);
  14.  
    System.out.println(result); // true
  15.  
     
  16.  
    boolean result2 = filter.test("java", 10);
  17.  
    System.out.println(result2); // false
  18.  
    }
  19.  
     
  20.  
    }

输出量

  1.  
    true
  2.  
    false
 

2. BiPredicate作为函数参数。

本示例使用BiPredicate通过域名或威胁分数过滤不良域名。

JavaBiPredicate2.java
  1.  
    package com.mkyong.java8;
  2.  
     
  3.  
    import java.util.Arrays;
  4.  
    import java.util.List;
  5.  
    import java.util.function.BiPredicate;
  6.  
    import java.util.stream.Collectors;
  7.  
     
  8.  
    public class JavaBiPredicate2 {
  9.  
     
  10.  
    public static void main(String[] args) {
  11.  
     
  12.  
    List<Domain> domains = Arrays.asList(new Domain("google.com", 1),
  13.  
    new Domain("i-am-spammer.com", 10),
  14.  
    new Domain("mkyong.com", 0),
  15.  
    new Domain("microsoft.com", 2));
  16.  
     
  17.  
    BiPredicate<String, Integer> bi = (domain, score) -> {
  18.  
    return (domain.equalsIgnoreCase("google.com") || score == 0);
  19.  
    };
  20.  
     
  21.  
    // if google or score == 0
  22.  
    List<Domain> result = filterBadDomain(domains, bi);
  23.  
    System.out.println(result); // google.com, mkyong.com
  24.  
     
  25.  
    // if score == 0
  26.  
    List<Domain> result2 = filterBadDomain(domains, (domain, score) -> score == 0);
  27.  
    System.out.println(result2); // mkyong.com, microsoft.com
  28.  
     
  29.  
    // if start with i or score > 5
  30.  
    List<Domain> result3 = filterBadDomain(domains, (domain, score) -> domain.startsWith("i") && score > 5);
  31.  
    System.out.println(result3); // i-am-spammer.com
  32.  
     
  33.  
    // chaining with or
  34.  
    List<Domain> result4 = filterBadDomain(domains, bi.or(
  35.  
    (domain, x) -> domain.equalsIgnoreCase("microsoft.com"))
  36.  
    );
  37.  
    System.out.println(result4); // google.com, mkyong.com, microsoft.com
  38.  
     
  39.  
     
  40.  
    }
  41.  
     
  42.  
    public static <T extends Domain> List<T> filterBadDomain(
  43.  
    List<T> list, BiPredicate<String, Integer> biPredicate) {
  44.  
     
  45.  
    return list.stream()
  46.  
    .filter(x -> biPredicate.test(x.getName(), x.getScore()))
  47.  
    .collect(Collectors.toList());
  48.  
     
  49.  
    }
  50.  
    }
  51.  
     
  52.  
    class Domain {
  53.  
     
  54.  
    String name;
  55.  
    Integer score;
  56.  
     
  57.  
    public Domain(String name, Integer score) {
  58.  
    this.name = name;
  59.  
    this.score = score;
  60.  
    }
  61.  
    // getters , setters , toString
  62.  
    }

输出量

  1.  
    [Domain{name='google.com', score=1}, Domain{name='mkyong.com', score=0}]
  2.  
    [Domain{name='mkyong.com', score=0}]
  3.  
    [Domain{name='i-am-spammer.com', score=10}]
  4.  
    [Domain{name='google.com', score=1}, Domain{name='mkyong.com', score=0}, Domain{name='microsoft.com', score=2}]

参考文献

 

翻译自: https://mkyong.com/java8/java-8-bipredicate-examples/

posted @ 2022-06-13 16:08  WPMA  阅读(996)  评论(0)    收藏  举报