1

Assume that I know that the list SomeList contains thatObj. Does the following code remove reference to thatObj from SomeList or not?

SomeClass el = (SomeClass) thatObj.clone();
SomeList.remove(el);

Can't find through the reference if this method compares objects somehow. Intuition suggests that it should use Object.equals which returns true if references point to the same object, hence this code will not work.

If not then an additional question: how to remove from list if don't have the reference but know all the members of the object in question?

  • Your intuition is exactly correct (unless you override `equals()`). – SLaks Nov 01 '13 at 17:15
  • `Object.equals` does not take the references in account, it takes the `equals` implementation in account which happen to be standardly using the reference. If you override `equals` with your own meaningful implementation, you can remove objects with the same data that have different references. – Jeroen Vannevel Nov 01 '13 at 17:17
  • Thanks! Do you know where this is described accurately, 'cos Oracle docs are ambiguous about this? – Igor Traskunov Nov 01 '13 at 17:22
  • @IgorTraskunov: the docs explicitly state that `equals` is used. `(o==null ? get(i)==null : o.equals(get(i)))` http://docs.oracle.com/javase/7/docs/api/java/util/List.html#remove(java.lang.Object) – Jeroen Vannevel Nov 01 '13 at 17:25

4 Answers4

1

Can't find through the reference if this method compares objects somehow. Intuition suggests that it should use Object.equals which returns true if references point to the same object, hence this code will not work.

Yes, you are right.

If not then an additional question: how to remove from list if don't have the reference but know all the members of the object in question?

Two possibilities:

  • override the equals method in your class, create a new instance with all known members and call remove passing the newly created instance as a parameter
  • iterate through all the objects inside the list and remove the one that has the members equal to the values you have
Adam Siemion
  • 15,569
  • 7
  • 58
  • 92
1

remove method internally uses the equals method to check for the object in the list. If equal returns true then it will be removed. Overriding the equals method will allow to remove the objects properly. For your reference here is the code of ArrayList remove method:

 public boolean remove(Object o) {
    if (o == null) {
            for (int index = 0; index < size; index++)
        if (elementData[index] == null) {
            fastRemove(index);
            return true;
        }
    } else {
        for (int index = 0; index < size; index++)
        if (o.equals(elementData[index])) {
            fastRemove(index);
            return true;
        }
        }
    return false;
    }
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

Override the equals method on the class - here is the javadoc. Also look at Overriding the java equals() method quirk and Overriding equals and hashCode in Java.

Community
  • 1
  • 1
Sualeh Fatehi
  • 4,700
  • 2
  • 24
  • 28
0

Search the list to find the member by returning the index, then get the object and remove it. You can also remove it by the index. The code

SomeList.indexOf() 

could help you to get the index of the object that override equals() and hashCode().