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);
}
}
}
求交集、並集和差集
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.