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
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...