Q:

Does equals apply on itself when looking an element in a list?

I have an object list with a position value (x, y). When I ask if an object of this list is equals to the current object, does the function return true when comparate to itself in this list?
Basically:
foreach(MyObject o : list){
    foreach(MyObject o2 : list){
        if(o.equals(o2))
            doSomething();
    }
}

@Override
public boolean equals(Object obj)
{
    if(obj instanceof MyObject) 
    {
        MyObject tmp = (MyObject)obj;
        return this.getPosition().equals(tmp.getPosition());
    }
    return false;
}

A:

If you wish your loop to avoid returning a false positive when comparing an element to itself, you can add to your condition:
for (MyObject o : list){
    for (MyObject o2 : list){
        if (o != o2 && o.equals(o2))
            doSomething();
    }
}

This will ensure that only two distinct instances (which means o and o2 don't refer to the same object) which are equal to each other will trigger doSomething().

