Something like 'contains any' for Java set?

前端 未结 9 762
余生分开走
余生分开走 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:31

    Use retainAll() in the Set interface. This method provides an intersection of elements common in both sets. See the API docs for more information.

提交回复
热议问题