guava中的交集、並集、差集


 @Test
    public static void testSets() {
        Set<Integer> set1 = Sets.newHashSet(1, 2, 4, 5, 6, 8);
        Set<Integer> set2 = Sets.newHashSet(2, 3, 4, 5, 6, 7, 9);
 
        //合集,並集   [1, 2, 4, 5, 6, 8, 3, 7, 9]
        Set<Integer> result1 = Sets.union(set1, set2);
        //交集          [2, 4, 5, 6]
        Set<Integer> result2 = Sets.intersection(set1, set2);
        //差集 1中有而2中沒有的  [1, 8]
        Set<Integer> result3 = Sets.difference(set1, set2);
        //相對差集 1中有2中沒有  2中有1中沒有的 取出來做結果 [1, 8, 3, 7, 9]
        Set<Integer> result4 = Sets.symmetricDifference(set1, set2);
 
        System.out.println(result1);
        System.out.println(result2);
        System.out.println(result3);
        System.out.println(result4);
 
    }

來自:https://blog.csdn.net/u012175512/article/details/105070668


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM