我如何检查是否一个ArrayList包含另一个ArrayList的任何元素? [英] How can I check if an ArrayList contains any element of another ArrayList?
本文介绍了我如何检查是否一个ArrayList包含另一个ArrayList的任何元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法来确定一个ArrayList是否包含不同的ArrayList中的任何元素?
Is there any way to determine whether an ArrayList contains any element of a different ArrayList?
这样的:
list1.contains(any element of list2)
时通过列表2
的所有元素循环和一个检查的元素之一,只有这样?
Is looping through all the elements of list2
and checking the elements one by one the only way?
推荐答案
虽然不是高效率的,这是简洁,采用了API:
Although not highly efficient, this is terse and employs the API:
if (!new HashSet<T>(list1).retainAll(list2).isEmpty())
// at least one element is shared
这篇关于我如何检查是否一个ArrayList包含另一个ArrayList的任何元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文