一 实现集合的并集、交集、差集
import java.util.HashSet;
import java.util.Set;
import java.util.Scanner;
public class Jihe {
public static void main(String[] args) {
Set result = new HashSet<>();
Set A = new HashSet();
Set B = new HashSet();
System.out.println("输入A,B各集合的元素个数,再依次输入各集合中的元素:");
Scanner scanner=new Scanner(System.in);
int x=scanner.nextInt();
int y=scanner.nextInt();
for(int i=0;i<x;i++)
{
int i1=scanner.nextInt();
A.add(i1);
}
for(int j=0;j<y;j++)
{
int j1=scanner.nextInt();
B.add(j1);
}
System.out.println("A = "+A.toString());
System.out.println("B = "+B.toString());
result.addAll(A);
result.retainAll(B);
System.out.println("交集 = " + result);
result.addAll(A);
result.removeAll(B);
System.out.println("差集 = "+result);
result.addAll(A);
result.addAll(B);
System.out.println("并集 = "+result);
}
}
**加粗样式**