ArrayList 不使用覆盖的 equals [英] ArrayList not using the overridden equals

查看:26
本文介绍了ArrayList 不使用覆盖的 equals的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在获取 ArrayList 以正确使用覆盖的 equals 时遇到问题.问题是我试图使用 equals 仅测试单个键字段,并使用 ArrayList.contains() 测试具有正确字段的对象是否存在.这是一个例子

I'm having a problem with getting an ArrayList to correctly use an overriden equals. the problem is that I'm trying to use the equals to only test for a single key field, and using ArrayList.contains() to test for the existence of an object with the correct field. Here is an example

public class TestClass  {
    private static class InnerClass{    
    private final String testKey;
    //data and such

    InnerClass(String testKey, int dataStuff) {
        this.testKey =testKey;
        //etc
    }
    @Override
    public boolean equals (Object in) {
        System.out.println("reached here");
        if(in == null) {
        return false;
        }else if( in instanceof String) {
        String inString = (String) in;
        return testKey == null ? false : testKey.equals(inString);
        }else {
        return false;
        }       
    }       
    }

    public static void main(String[] args) {    
    ArrayList<InnerClass> objectList = new ArrayList<InnerClass>();
    //add some entries
    objectList.add(new InnerClass("UNIQUE ID1", 42));
    System.out.println( objectList.contains("UNIQUE ID1")); 
    }    
}

让我担心的是,我不仅在输出上出错,而​​且我也没有得到到达此处"的输出.

What worries me is that not only am I getting false on the output, but I'm also not getting the "reached here" output.

有没有人知道为什么这个覆盖被完全忽略了?覆盖和内部类是否有一些我不知道的微妙之处?

Does anyone have any ideas why this override is being completely ignored? Is there some subtlety with overrides and inner classes I don't know of?

网站有问题,所以我似乎无法标记答案.感谢您的快速回复:是的,我的疏忽是调用了 String .equals thta,而不是我自定义的.我想现在是老式的支票

Having problems with the site so I cant seem to mark the answered. Thanks for the quick response: yes an oversight on my part that it is the String .equals thta is called, not my custom one. I guess it's old fashioned checks for now

推荐答案

如果你查看ArrayList的源码,你会发现它调用了其他对象的equals.在您的情况下,它将调用 String "UNIQUE ID1"equals ,这将检查其他对象是否属于 String 类型并仅返回 >假:

If you check sources of ArrayList, you will see that it calls equals of other object. In your case it will call equals of String "UNIQUE ID1" which will check that other object is not of type String and just returns false:

public boolean contains(Object o) {
    return indexOf(o) >= 0;
}

public int indexOf(Object o) {
    ...     
    for (int i = 0; i < size; i++)
    if (o.equals(elementData[i]))
        return i;
    ...
    return -1;
}

对于你的案例调用 containsInnerClass 只包含 id:

For your case call contains with InnerClass that only contains id:

objectList.contains(new InnerClass("UNIQUE ID1"))

不要忘记为 InnerClass 实现 equals,它只比较 id.

Don't forget to implement equals for InnerClass which compares id only.

这篇关于ArrayList 不使用覆盖的 equals的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆