Why are the methods contains() and indexOf() in the Java collections framework defined using o.equals(e) and not e.equals(o) (where o is the argument of the methods and e is the element in the collection)?
Anyone know the reasons of that?
Because
ois known not be null, buteisn’t necessarily. Take this example from the code forLinkedList:In this example, doing it this way round avoids the need to protect against
e.elementbeing null for every item in the collection. Here’s the full code that takes account ofobeing null: