谓词方法等于()

发布于 2024-11-07 13:37:41 字数 822 浏览 3 评论 0原文

我正在使用来自 com.google.common.base (Google Guava) 的接口 Predicate

但我不知道如何让 等于() 方法有效...

为什么当我输入这样的内容时会得到 false

    Predicate<Object> PredicateD = new Predicate<Object>(){
          @Override public boolean apply(Object number) {
                return ((number instanceof Double) && (Math.floor((Double)number) == (Double)number));
            }    
    };

    Predicate<Object> PredicateM = new Predicate<Object>(){
          @Override public boolean apply(Object number) {
                return ((number instanceof Double) && (Math.floor((Double)number) == (Double)number));
            }    
    };

    System.out.println(PredicateD.equals(PredicateM));

提前感谢您的帮助,

I'm using the interface Predicate<T> from com.google.common.base (Google Guava)

But I don't know how to have the equals() method works...

Why do I get false when I type something like this :

    Predicate<Object> PredicateD = new Predicate<Object>(){
          @Override public boolean apply(Object number) {
                return ((number instanceof Double) && (Math.floor((Double)number) == (Double)number));
            }    
    };

    Predicate<Object> PredicateM = new Predicate<Object>(){
          @Override public boolean apply(Object number) {
                return ((number instanceof Double) && (Math.floor((Double)number) == (Double)number));
            }    
    };

    System.out.println(PredicateD.equals(PredicateM));

Thanks in advance for your Help,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睡美人的小仙女 2024-11-14 13:37:41

您正在创建两个不同的匿名内部类,但它们都没有重写equals,因此您将获得默认实现,其中任何两个不相等的引用都会被考虑不相等。

由于 PredicateDPredicateM 的值是不同的引用,因此 equals 返回 false。

You're creating two different anonymous inner classes, but neither of them is overriding equals, so you'll get the default implementation, whereby any two non-equal references are considered non-equal.

As the values of PredicateD and PredicateM are different references, equals returns false.

童话里做英雄 2024-11-14 13:37:41

为了让它打印 true,您必须重写这些类上的 equals 方法。您的自定义 equals 必须检查参数是否是这些类中任一类的实例。由于它们是匿名的,因此您无法引用它们,因此您无法真正做到这一点。

我的建议是创建一个非匿名类(例如,IntegerCheckingPredicate),并创建该类的 predicateDpredicateM 实例。那么你的 equals 方法可能看起来像:

public boolean equals(Object o) {
    if (o instanceof IntegerCheckPredicate) {
        return true;
    }

    return false;
}

那么这个测试就会通过:

@Test
public void testPredicatesWithEqualsOverriddenAreEqual() {
    IntegerCheckPredicate predicateM = new IntegerCheckPredicate();
    IntegerCheckPredicate predicateD = new IntegerCheckPredicate();
    assertEquals(predicateM, predicateD);
}

In order to get this to print true, you would have to override the equals method on these classes. Your custom equals would have to check if the parameter is an instance of either of these classes. Since they are anonymous, you'd have no way of referencing them, so you can't really do that.

My suggestion would be to make a non-anonymous class (say, IntegerCheckingPredicate), and make predicateD and predicateM instances of that class. Then your equals method might look like:

public boolean equals(Object o) {
    if (o instanceof IntegerCheckPredicate) {
        return true;
    }

    return false;
}

Then this test would pass:

@Test
public void testPredicatesWithEqualsOverriddenAreEqual() {
    IntegerCheckPredicate predicateM = new IntegerCheckPredicate();
    IntegerCheckPredicate predicateD = new IntegerCheckPredicate();
    assertEquals(predicateM, predicateD);
}
春庭雪 2024-11-14 13:37:41

我认为没有必要重写 equals()。谓词是函数类。 Object.equals(){return (this == obj);} 是合理的。

I don't think it's necessary to override equals(). Predicate is functional class. The Object.equals(){return (this == obj);} is reasonable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文