爪哇.ArrayList 的 contains 方法不起作用 [英] Java. ArrayList's contains method not working

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

问题描述

我正在检查 ArrayList 是否包含对象:

I am checking if ArrayList contains object:

            List<Property> propertiesByName = getPropertiesByCategory(propertyCategory);
        for(Property property: propertiesByName){
            List<Property> propertyList = getVariationItem().getProperties();
            Galgo.log("*******************");
            Galgo.log(propertyList.toString());
            Galgo.log(property.toString());
            Galgo.log("contains:"+propertyList.contains(property));
        }

我收到以下日志:

*******************
[Property{name='color', value='red'}, Property{name='size', value='42'}]
Property{name='color', value='red'}
contains:false
*******************
[Property{name='color', value='red'}, Property{name='size', value='42'}]
Property{name='color', value='blue'}
contains:false
Database: get 2 variations
*******************
[Property{name='color', value='red'}, Property{name='size', value='42'}]
Property{name='size', value='42'}
contains:false
*******************
[Property{name='color', value='red'}, Property{name='size', value='42'}]
Property{name='size', value='34'}
contains:false

正如您在第一种和第三种情况中所见,它应该返回 true.怎么了?

As you can see in the first and third cases, it should return true. What is wrong?

我的代码的其他部分.按类别(颜色、大小)获取属性的第一种方法.第二种方法是获取所有可用的属性:

Other parts of my code. First method to get properties by category(color, size). Second method is to get all available properties:

            private List<Property> getPropertiesByCategory(String category){
            List<Property> properties = new ArrayList<>();
            for(Property property: getAllProperties()){
                if(property.getName().equals(category)){
                    if(!properties.contains(property)){
                        properties.add(property);
                    }
                }
            }
            return properties;
        }

        private List<Property> getAllProperties() {
            List<Property> propertyList = new ArrayList<>();
            for(VariationItem variationItem: getProductItem().getVariationsList()){
                for(Property property: variationItem.getProperties()){
                    if(!propertyList.contains(property))
                    {
                        propertyList.add(property);
                    }
                }
            }
            return propertyList;
        }

推荐答案

要使用 contains 方法,必须重写 equals()hashCode() 方法来实现这一点.您可以查看此答案以了解实施 https://stackoverflow.com/a/16069158/1320616.实际上 contains() 会比较两个对象.并且要比较两个对象,您必须实现 equals() 方法.

To use contains method, you have to override equals() and hashCode() methods to achieve this. You can check this answer for implementation https://stackoverflow.com/a/16069158/1320616. Actually contains() will compare two objects. And to compare two objects you have to implement equals() method.

这里是完整的细节

所以当你使用 contains() 时,它正在做的是

So when you use contains() what it is doing is

@Override public boolean contains(Object object) {
        Object[] a = array;
        int s = size;
        if (object != null) {
            for (int i = 0; i < s; i++) {
                if (object.equals(a[i])) {
                    return true;
                }
            }
        } else {
            for (int i = 0; i < s; i++) {
                if (a[i] == null) {
                    return true;
                }
            }
        }
        return false;
    }

如果你没有在你的类中实现 equals() 方法,它将从 Object 类中获取 equals() 方法,即

and if you don't implement equals() method in your class it will take the equals() method from the Object class which is

public boolean equals(Object o) {
        return this == o;
}

所以现在它减少到 == 被用于两个对象之间来比较它们.当您将 == 放在两个对象之间时,它会根据两件事(a)两个对象的哈希码(b)它使用两个对象的 toString() 进行比较.

So now it reduces to == is being used between two objects to compare them. When you put == between two objects, it compares on basis of two things (a) hashcodes of two objects (b) It uses toString() of the two objects.

每个对象都分配有不同的哈希码.这就是你的 contains() 没有给你正确结果的原因.

Each object has different hashcodes assigned to them. this is the reason why your contains() is not giving you correct results.

这篇关于爪哇.ArrayList 的 contains 方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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