public static < T > boolean anyValuesInCommon ( Collection < T > c1 , Collection < T > c2 ) { // Let's always iterate over smaller collection: if ( c1 . size ( ) > c2 . size ( ) ) { Collection < T > tmp = c1 ; c1 = c2 ; c2 = tmp ; } Iterator < T > it = c1 . iterator ( ) ; while ( it . hasNext ( ) ) { if ( c2 . contains ( it . next ( ) ) ) { return true ; } } return false ; }
Method that can be used to efficiently check if 2 collections share at least one common element .