我有两个arraylists,其中每个arraylists存储一组元素.我想获得并输出这两组的交集.有没有任何有效和优雅的方法来实现这一目标?工会怎么样?
HashSet s0 = new HashSet(arraylist0);
s0.retainAll(arraylist1);
System.out.println("Intersection: " + s0);
s0 = new HashSet(arraylist0);
s0.addAll(arraylist1);
System.out.println("Union: " + s0);
Run Code Online (Sandbox Code Playgroud)