Symmetric difference of two sets in Java

前端 未结 7 2002
礼貌的吻别
礼貌的吻别 2021-02-03 21:49

There are two TreeSets in my app:

set1 = {501,502,503,504}
set2 = {502,503,504,505}

I want to get the symmetric difference of thes

7条回答
  •  北恋
    北恋 (楼主)
    2021-02-03 22:34

    Set s1 = new HashSet();
        Set s2 = new HashSet();
        s1.add("a");
        s1.add("b");
        s2.add("b");
        s2.add("c");
        Set s3 = new HashSet(s1);
        s1.removeAll(s2);
        s2.removeAll(s3);
        s1.addAll(s2);
        System.out.println(s1);
    

    output of s1 : [a,c]

提交回复
热议问题