How does a ArrayList's contains() method evaluate objects?

后端 未结 9 1017
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 10:32

Say I create one object and add it to my ArrayList. If I then create another object with exactly the same constructor input, will the contains() me

相关标签:
9条回答
  • 2020-11-22 10:54

    I think that right implementations should be

    public class Thing
    {
        public int value;  
    
        public Thing (int x)
        {
            this.value = x;
        }
    
        @Override
        public boolean equals(Object object)
        {
            boolean sameSame = false;
    
            if (object != null && object instanceof Thing)
            {
                sameSame = this.value == ((Thing) object).value;
            }
    
            return sameSame;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 10:54

    The ArrayList uses the equals method implemented in the class (your case Thing class) to do the equals comparison.

    0 讨论(0)
  • 2020-11-22 10:56

    Shortcut from JavaDoc:

    boolean contains(Object o)

    Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e))

    0 讨论(0)
  • 2020-11-22 10:57

    Just wanted to note that the following implementation is wrong when value is not a primitive type:

    public class Thing
    {
        public Object value;  
    
        public Thing (Object x)
        {
            this.value = x;
        }
    
        @Override
        public boolean equals(Object object)
        {
            boolean sameSame = false;
    
            if (object != null && object instanceof Thing)
            {
                sameSame = this.value == ((Thing) object).value;
            }
    
            return sameSame;
        }
    }
    

    In that case I propose the following:

    public class Thing {
        public Object value;  
    
        public Thing (Object x) {
            value = x;
        }
    
        @Override
        public boolean equals(Object object) {
    
            if (object != null && object instanceof Thing) {
                Thing thing = (Thing) object;
                if (value == null) {
                    return (thing.value == null);
                }
                else {
                    return value.equals(thing.value);
                }
            }
    
            return false;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 10:58

    ArrayList implements the List Interface.

    If you look at the Javadoc for List at the contains method you will see that it uses the equals() method to evaluate if two objects are the same.

    0 讨论(0)
  • 2020-11-22 11:00

    Generally you should also override hashCode() each time you override equals(), even if just for the performance boost. HashCode() decides which 'bucket' your object gets sorted into when doing a comparison, so any two objects which equal() evaluates to true should return the same hashCode value(). I cannot remember the default behavior of hashCode() (if it returns 0 then your code should work but slowly, but if it returns the address then your code will fail). I do remember a bunch of times when my code failed because I forgot to override hashCode() though. :)

    0 讨论(0)
提交回复
热议问题