Something like 'contains any' for Java set?

前端 未结 9 766
余生分开走
余生分开走 2020-11-28 01:52

I have two sets, A and B, of the same type.

I have to find if A contains any element from the set B.

What would be the best way to do that without iterating

9条回答
  •  误落风尘
    2020-11-28 02:34

    I use org.apache.commons.collections.CollectionUtils

    CollectionUtils.containsAny(someCollection1, someCollection2)
    

    That is All! Returns true if at least one element is in both collections.

    Simple to use, and the name of the function is more suggestive.

提交回复
热议问题