Check if a collection contains an object, comparing by reference

前端 未结 6 1533
猫巷女王i
猫巷女王i 2020-12-29 22:13

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

From Java7 Javadoc

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-29 23:12

    For those of us using Java 8, Collection#stream() is a clean option:

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

提交回复
热议问题