[java]代码库
package s0222交集并集差集;
import java.util.Set;
import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;
public class Main {
public static void main(String[] args) {
Set<Integer> set1=Sets.newHashSet(1,2,3,4,5,6,7);
Set<Integer> set2=Sets.newHashSet(1,5,3,4,0,15,8);
//交集
SetView<Integer> sv1=Sets.intersection(set1, set2);
System.out.println("\n"+"交集为:");
sv1.forEach(element->System.out.print(element+","));
//差集
SetView<Integer> sv2=Sets.difference(set1, set2);
System.out.println("\n"+"差集为:");
sv2.forEach(element->System.out.print(element+","));
//并集
SetView<Integer> sv3=Sets.union(set1, set2);
System.out.println("\n"+"并集为:");
sv3.forEach(element->System.out.print(element+","));
}
}