import java.util.*;
class Test
{
public static void main(String args[])
{
String num1[]={"Tom","Jim","John","Lucy","Jane","Lizz"};
String num2[]={"Katie","John","Lucy","Machelle","George"};
HashSet<String> A=new HashSet<String>(Arrays.asList(num1));
HashSet<String> B=new HashSet<String>(Arrays.asList(num2));
HashSet<String> Aa=new HashSet<String>(A);
HashSet<String> Bb=new HashSet<String>(B);
/*
HashSet<String> Aa=new HashSet<String>(A);
HashSet<String> Bb=new HashSet<String>(B);
可写成
HashSet<String> Aa=new HashSet<String>();
HashSet<String> Bb=new HashSet<String>();
Aa.addAll(A);
Bb.addAll(B);
*/
System.out.println("交集结果为 ");
if(A.retainAll(B)==true)
{
System.out.println(A);
}
A=Aa;
B=Bb;
System.out.println("并集结果为 ");
if(A.addAll(B)==true)
{
System.out.println(A);
}
A=Aa;
B=Bb;
System.out.println("差集结果为 ");
if(A.removeAll(B)==true)
{
System.out.println(A);
}
}
}
求交集、并集和差集
最新推荐文章于 2023-04-06 10:44:37 发布