java - Check if a collection contains an object, comparing by reference -


the collection.contains() method check if collection contains given object, using .equals() method perform comparison.

from java7 javadoc:

boolean contains(object o)

returns true if collection contains specified element. more formally, returns true if , if collection contains @ least 1 element e such (o==null ? e==null : o.equals(e)).

is there smart way check if collection contains object o, comparing reference instead (i.e. o==e)?

of course can iterate through collection , make check, i'm looking existing function can that.

clarifications:

  • i want perform operation regardless equals() implementation of object in collection.
  • i don't want change objects in collection nor collection itself.

edit:

even though question general solution collection implementations, specific cases collection sub-interfaces appreciated.

for of using java 8, collection#stream() clean option:

collection.stream().anymatch(x -> x == key) 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -