public static < T > Collection < T > intersect ( Collection < T > first , Collection < T > second ) { E . checkNotNull ( first , "first" ) ; E . checkNotNull ( second , "second" ) ; HashSet < T > results = null ; if ( first instanceof HashSet ) { @ SuppressWarnings ( "unchecked" ) HashSet < T > clone = ( HashSet < T > ) ( ( HashSet < T > ) first ) . clone ( ) ; results = clone ; } else { results = new HashSet <> ( first ) ; } results . retainAll ( second ) ; return results ; }
Find the intersection of two collections the original collections will not be modified