How can I check if an ArrayList contains any element of another ArrayList? [duplicate]
问题 This question already has answers here : Check if one list contains element from the other (11 answers) Closed 5 years ago . Is there any way to determine whether an ArrayList contains any element of a different ArrayList? Like this: list1.contains(any element of list2) Is looping through all the elements of list2 and checking the elements one by one the only way? 回答1: Although not highly efficient, this is terse and employs the API: if (!new HashSet<T>(list1).retainAll(list2).isEmpty()) //