美文网首页
Guava之Sets

Guava之Sets

作者: 神豪VS勇士赢 | 来源:发表于2020-08-26 08:57 被阅读0次
public class SetsExampleTest
{

    @Test
    public void testCreate()
    {
        HashSet<Integer> set = Sets.newHashSet(1, 2, 3);
        assertThat(set.size(), equalTo(3));

        ArrayList<Integer> list = Lists.newArrayList(1, 1, 2, 3);
        assertThat(list.size(), equalTo(4));

        HashSet<Integer> set2 = Sets.newHashSet(list);
        assertThat(set2.size(), equalTo(3));


    }

    @Test
    public void testCartesianProduct()
    {

        Set<List<Integer>> set = Sets.cartesianProduct(Sets.newHashSet(1, 2), Sets.newHashSet(3, 4), Sets.newHashSet(5, 6));

        System.out.println(set);


    }

    @Test
    public void testCombinations()
    {
        HashSet<Integer> set = Sets.newHashSet(1, 2, 3);
        Set<Set<Integer>> combinations = Sets.combinations(set, 2);
        combinations.forEach(System.out::println);
    }

    @Test
    public void testDiff()
    {
        HashSet<Integer> set1 = Sets.newHashSet(1, 2, 3);
        HashSet<Integer> set2 = Sets.newHashSet(1, 4, 6);
        Sets.SetView<Integer> diffResult1 = Sets.difference(set1, set2);
        System.out.println(diffResult1);
        Sets.SetView<Integer> diffResult2 = Sets.difference(set2, set1);
        System.out.println(diffResult2);


    }

    @Test
    public void testIntersection()
    {
        HashSet<Integer> set1 = Sets.newHashSet(1, 2, 3);
        HashSet<Integer> set2 = Sets.newHashSet(1, 4, 6);
        Sets.intersection(set1, set2).forEach(System.out::println);
    }

    @Test
    public void testUnionSection()
    {
        HashSet<Integer> set1 = Sets.newHashSet(1, 2, 3);
        HashSet<Integer> set2 = Sets.newHashSet(1, 4, 6);
        Sets.union(set1, set2).forEach(System.out::println);
    }
}

相关文章

网友评论

      本文标题:Guava之Sets

      本文链接:https://www.haomeiwen.com/subject/rybojktx.html