The Collection.contains() method check if a collection contains a given object, using the .equals()
method to perform the comparison.
From Java7 Javadoc:
boolean contains(Object o)
Returns true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e)).
Is there a smart way to check if a collection contains an object o
, but comparing by reference instead (i.e. o==e
)?
Of course I can iterate through the collection and make the check, I'm looking for an existing function which can do that.
Clarifications:
- I want to perform this operation regardless the
equals()
implementation of the object in the collection. - I don't want to change the objects in the collection nor the collection itself.
Edit:
Even though my question is about a general solution for Collection
implementations, specific cases for Collection
sub-interfaces would also be appreciated.