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