【问题标题】:Java how to compare PredicatesJava如何比较谓词
【发布时间】:2020-07-13 08:51:56
【问题描述】:

我有两个prodicates:

Predicate<CategoryModel> predicate1 = NavigationCategoryModel.class::isInstance;
Predicate<CategoryModel> predicate2 = BrandCategoryModel.class::isInstance;

使用 and if 语句,我如何确定我使用的是哪个谓词?我正在尝试做这样的事情,但显然没有编译:

if(predicate1.equals(NavigationCategoryModel.class::isInstance)){
}

if(predicate1==NavigationCategoryModel.class::isInstance){
}

有什么提示吗?我对 Java 8 lambdas 很陌生

这是 Pojos 的代码(用于测试目的的简单继承):

public class CategoryModel {
}

public class NavigationCategoryModel  extends CategoryModel{
}

public class BrandCategoryModel extends CategoryModel {
}

【问题讨论】:

  • 您到底想比较他们什么?如果它们是相同类型的对象?
  • 听起来像是 XY 问题。为什么需要这个?
  • 我想比较一下他们是使用 NavigationCategoryModel 还是 BrandCategoryModel 的谓词
  • 我需要知道哪个 isInstance 类正在使用
  • 当您将声明替换为语义上等效的Predicate&lt;CategoryModel&gt; predicate1 = new Predicate&lt;CategoryModel&gt;() { public boolean test(CategoryModel o) { return o instanceof NavigationCategoryModel;} }; 时,是否有任何方法可以发现它正在测试instanceof NavigationCategoryModel?您是否希望 predicate1.equals(new Predicate&lt;CategoryModel&gt;() { public boolean test(CategoryModel o) { return o instanceof NavigationCategoryModel;} }) 评估为 true?是否有任何理由假设 lambda 表达式或方法引用的行为不同?

标签: java java-8 compare java-stream predicate


【解决方案1】:

您应该在谓词上使用test 方法。而且,您必须提供对象来执行验证,而不是提供实际的方法参考

predicate.test(object)

文档:Predicate#test

对于您的问题,您可以测试 predicate1 在 object 类型为 NavigationCategoryModel 时是否返回 true,如下所示:

predicate1.test(new NavigationCategoryModel()) // returns true

同样,对于BrandCategoryModel,使用:

predicate2.test(new BrandCategoryModel()) // returns true

如果您想测试该对象是否匹配两个中的任何一个,您可以将两个谓词结合起来,例如:

predicate1.or(predicate2).test(new NavigationCategoryModel()) // returns true
predicate1.or(predicate2).test(new BrandCategoryModel()) // returns true

【讨论】:

    【解决方案2】:

    您尝试的是找到您使用的实现。

    唯一的方法是使用来自Predicate的函数test

    true if the input argument matches the predicate, otherwise false
    
    public static void main(String args[]) {
    
        Predicate<CategoryModel> predicate1 = NavigationCategoryModel.class::isInstance;
        Predicate<CategoryModel> predicate2 = BrandCategoryModel.class::isInstance;
    
        System.out.println("Predicate1 isNavigation: " + isNavigation(predicate1));
        System.out.println("Predicate1 isBrand: " + isBrand(predicate1));
        System.out.println("--------------------------------------------------");
        System.out.println("Predicate2 isNavigation: " + isNavigation(predicate2));
        System.out.println("Predicate2 isBrand: " + isBrand(predicate2));
    
    }
    
    public static boolean isNavigation(Predicate<CategoryModel> predicate){
    
        return predicate.test(new NavigationCategoryModel());
    
    }
    
    public static boolean isBrand(Predicate<CategoryModel> predicate){
    
        return predicate.test(new BrandCategoryModel());
    
    }
    

    【讨论】:

      【解决方案3】:

      就像昆仑的解决方案,但我认为你应该多加一个条件,例如

      Predicate<CategoryModel> predicate1 = NavigationCategoryModel.class::isInstance;
      Predicate<CategoryModel> predicate2 = BrandCategoryModel.class::isInstance;
      
      Predicate<CategoryModel> predicate1Testing = NavigationCategoryModel.class::isInstance;
      
      System.out.println("Is A NavigationCategoryModel Predicate? " + predicate1.and(predicate1Testing).test(new NavigationCategoryModel()));
      

      【讨论】:

      • 我不明白。 predicate1predicate1Testing 有什么区别?不知道为什么将一个与另一个扩展是一个优势。
      • 嗨@Scratte,谓词1Testing 可能不同。在这个问题中,作者想知道使用的是哪个谓词? predicate1Testing 是与原始谓词进行比较的输入。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-10
      • 2018-10-23
      • 1970-01-01
      • 2013-06-04
      • 1970-01-01
      • 1970-01-01
      • 2019-09-19
      相关资源
      最近更新 更多