It is defined in JavaDoc:
boolean contains(Object o)
Returns
true
if this collection contains the specified element. More formally, returnstrue
if and only if this collection contains at least one element e such that(o==null ? e==null : o.equals(e))
.
In words: If o
is null
, contains will return true
if the collection contains a null
element, otherwise it will return true
if and only if there is an element that equals()
the one you are searching for.
Every call to new Integer(5)
will return a different reference, but all the objects are equal to one another according to the equals()
method of the Integer
class:
Compares this object to the specified object. The result is
true
if and only if the argument is notnull
and is anInteger
object that contains the sameint
value as this object.