Guava - 强大的集合工具Sets

一、简介

Sets类是Guava提供来处理Set相关的非常实用的工具类.它的一些静态工厂方法、实用的集合操作方法会让我们编程起来非常的流畅、优雅.很多较常用的集合操作场景无需我们再去手动实现.

二、标准的集论操作 - Set-Theoretic Operations

Guava提供了一些标准的集论操作,作为参数集上的视图来实现。这些返回一个SetView,可以使用它:

1、直接作为一个集合,因为它实现了集合接口

2、通过使用copyInto(Set)将它复制到另一个可变集合中

3、通过使用immutableCopy()创建不可变副本

三、常用的实用方法

1、取并集方法 - union()

Sets工具类提供了两个Set集合取并集的方法 - union(final Set set1, final Set set2),返回两个集合合并后的SetView视图.

实验代码:

        Set union1 = Sets.newHashSet(1, 2, 3, 7, 8, 9);
        Set union2 = Sets.newHashSet(2, 3, 6, 7, 10);
        //测试union方法,取并集
        Sets.SetView unionSetView = Sets.union(union1, union2);
        System.out.println("=================测试union方法====================" + unionSetView);
        //值得注意的是取并集的两个Set任何一个都不能为Null
        //值得注意的是 - 取并集之后的顺序没有经过排序,而是根据left union1的原始顺序之后往后追加right union2中的原始顺序值.

实验结果:

=================测试union方法====================[1, 2, 3, 7, 8, 9, 10, 6]

可以发现两个set之间做了取并集的操作,并且数字的大小并未进行排序.

值得注意的是:

1、取并集的两个Set任何一个都不能为Null.
2、取并集之后的顺序没有经过排序,而是根据left set1的原始顺序之后往后追加right set2中的原始顺序值.

2、取交集的方法 - intersection()

Sets工具类提供了两个Set集合取交集的方法 - intersection(final Set set1, final Set set2),返回两个集合交集后的SetView视图.

实验代码:

        //测试intersection方法,取交集.
        Set interSection1 = Sets.newHashSet(1, 2, 3, 7, 8, 9);
        Set interSection2 = Sets.newHashSet(2, 3, 6, 7, 10);

        Sets.SetView intersectionSetView = Sets.intersection(interSection1, interSection2);
        System.out.println("=================测试intersection()方法====================" + intersectionSetView);

实验结果:

=================测试intersection()方法====================[2, 3, 7]
值得注意的是:

1、取交集的两个Set任何一个都不能为Null

3、单向diff方法 - difference()

Sets支持两个集合之间做单向diff的功能,提供了便捷方法 - difference(final Set set1, final Set set2).

实验代码:

        //测试difference方法
        Set difference1 = Sets.newHashSet(1, 2, 3, 7, 8, 9);
        Set difference2 = Sets.newHashSet(2, 3, 6, 7, 10);

        Sets.SetView differenceSetView = Sets.difference(difference1, difference2);
        System.out.println("=================测试difference()方法====================" + differenceSetView);
        //注意 - diff的结果是以set1为基准,得到的结果是set1中比对set2,如果set1有不同于set2的,返回此类数据的SetView

实验结果:

=================测试difference()方法====================[1, 8, 9]
值得注意的是:

1、做diff的两个Set任何一个都不能为Null
2、diff的结果是单向的 - 以left - set1为基准,得到的结果是left - set1中比对right - set2,如果set1有不同于set2的元素,返回此类数据的SetView.

4、双向对称diff方法 - symmetricDifference()

Sets还支持两个集合之间做双向对称diff的功能,提供了便捷方法 - symmetricDifference( final Set set1, final Set set2).

实验代码:

        //测试symmetricDifference方法 - 双向对称diff功能
        Set symmetricDifference1 = Sets.newHashSet(1, 2, 3, 7, 8, 9);
        Set symmetricDifference2 = Sets.newHashSet(2, 3, 6, 7, 10);

        Sets.SetView symmetricDifferenceSetView = Sets.symmetricDifference(symmetricDifference1, symmetricDifference2);
        System.out.println("=================测试symmetricDifference()方法====================" + symmetricDifferenceSetView);

实验结果:

=================测试symmetricDifference()方法====================[1, 8, 9, 10, 6]

可以发现得到的结果是两个集合对称diff之后的结果.

值得注意的是:

1、做diff的两个Set任何一个都不能为Null

5、求笛卡尔积方法 - cartesianProduct()

如同Lists提供求笛卡尔积方法一样,Sets工具类也提供了求笛卡尔积的方法 - cartesianProduct().

实验代码:

        //测试cartesianProduct方法 - 求笛卡尔积功能
        Set cartesianProduct1 = Sets.newHashSet(1, 2);
        Set cartesianProduct2 = Sets.newHashSet(2, 3, 6, 7, 10);

        Set> cartesianProductSet = Sets.cartesianProduct(cartesianProduct1, cartesianProduct2);
        System.out.println("=================测试cartesianProduct()方法====================" + cartesianProductSet);

实验结果:

=================测试cartesianProduct()方法====================[[1, 2], [1, 10], [1, 3], [1, 6], [1, 7], [2, 2], [2, 10], [2, 3], [2, 6], [2, 7]]
6、获取指定集合所有子集方法 - powerSet()

Sets还支持求取指定集合所有子集的方法powerSet().

实验代码:

        //测试powerSet方法 - 求当前集合的所有子集
        Set powerSet1 = Sets.newHashSet(1, 2, 3);

        Set> powerSet = Sets.powerSet(powerSet1);
        System.out.println("=================测试powerSet()方法====================" + powerSet);
        powerSet.forEach(setData -> System.out.println("========当前set数据为=======" + setData));

实验结果:

=================测试powerSet()方法====================powerSet({1=0, 2=1, 3=2})
========当前set数据为=======[]
========当前set数据为=======[1]
========当前set数据为=======[2]
========当前set数据为=======[1, 2]
========当前set数据为=======[3]
========当前set数据为=======[1, 3]
========当前set数据为=======[2, 3]
========当前set数据为=======[1, 2, 3]

四、静态工厂方法

Sets工具类也提供了许多静态工厂方法方便我们更优雅的创建Set集合.官网描述如下:

Implementation Factories
HashSet basic, with elements, from Iterable, with expected size, from Iterator
LinkedHashSet basic, from Iterable, with expected size
TreeSet basic, with Comparator, from Iterable
1、HashSet相关静态工厂方法
        Sets.newHashSet();
        Sets.newConcurrentHashSet();
2、LinkedHashSet相关静态工厂方法
        Sets.newLinkedHashSet();
        Sets.newLinkedHashSetWithExpectedSize(NumberUtils.INTEGER_ONE);
3、TreeSet相关静态工厂方法
        Sets.newTreeSet();

......未完待续

你可能感兴趣的:(Guava - 强大的集合工具Sets)