获取Java中两组的不对称差异
使用removeAll()方法获得两组不对称差异。
第一组-
HashSet <String> set1 = new HashSet <String>();
set1.add("Mat");
set1.add("Sat");
set1.add("Cat");第二组-
HashSet <String> set2 = new HashSet <String>();
set2.add("Mat");获得不对称差异-
set1.removeAll(set2);
以下是显示如何获取两组之间的不对称差异的示例-
示例
import java.util.*;
public class Demo {
public static void main(String args[]) {
HashSet <String> set1 = new HashSet <String>();
HashSet <String> set2 = new HashSet <String>();
set1.add("Mat");
set1.add("Sat");
set1.add("Cat");
System.out.println("Set1 = "+ set1);
set2.add("Mat");
System.out.println("Set2 = "+ set2);
set1.removeAll(set2);
System.out.println("Asymmetric difference = "+ set1);
}
}输出结果
Set1 = [Mat, Sat, Cat] Set2 = [Mat] Asymmetric difference = [Sat, Cat]
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短