Guava collections -- Sets
来源:互联网 发布:手机点烟软件 编辑:程序博客网 时间:2024/06/05 06:49
Guava全文介绍地址:Google Guava
这次主要介绍是的是com.google.common.collect.Sets.主要的作用是就与Java Set相关的静态的实用方法。在Guava中与之类似的还有Lists,Maps以及Queues这些类。
下面就介绍一个Sets的主要用法:
1、笛卡尔集
@Test public void testCartesianProduct(){ Set<String> s1 = Sets.newHashSet("Foo","Bar"); Set<String> s2 = Sets.newHashSet("Jim","Bob"); Set<List<String>> cartesian = Sets.cartesianProduct(s1, s2); List<String> list = Lists.newArrayList(); list.add("Foo"); list.add("Jim"); assertThat(cartesian.contains(list),is(true)); list.clear(); list.add("Foo"); list.add("Bob"); assertThat(cartesian.contains(list),is(true)); list.clear(); list.add("Bar"); list.add("Jim"); assertThat(cartesian.contains(list),is(true)); list.clear(); list.add("Bar"); list.add("Bob"); assertThat(cartesian.contains(list),is(true)); }
2、返回2个set前一个包含后一个不包含的所有的元素
@Test public void testSetDifference() { Set<String> s1 = Sets.newHashSet("1","2","3"); Set<String> s2 = Sets.newHashSet("3","2","4"); Sets.SetView<String> sv = Sets.difference(s1,s2); assertThat(sv.size()==1 && sv.contains("1"),is(true)); sv = Sets.difference(s2,s1); assertThat(sv.size()==1 && sv.contains("4"),is(true)); }
3、返回2个set,共同元素以外的所有元素
@Test public void testSymmetricDifference(){ Set<String> s1 = Sets.newHashSet("1","2","3"); Set<String> s2 = Sets.newHashSet("3","2","4"); Sets.SetView<String> sv = Sets.symmetricDifference(s1,s2); assertThat(sv.size()==2 && sv.contains("1") && sv.contains("4"),is(true)); }
4、返回2个set的交集
@Test public void testIntersection(){ Set<String> s1 = Sets.newHashSet("1","2","3"); Set<String> s2 = Sets.newHashSet("3","2","4"); Sets.SetView<String> sv = Sets.intersection(s1,s2); assertThat(sv.size()==2 && sv.contains("2") && sv.contains("3"),is(true)); }
5、返回2个set的并集
@Test public void testUnion(){ Set<String> s1 = Sets.newHashSet("1","2","3"); Set<String> s2 = Sets.newHashSet("3","2","4"); Sets.SetView<String> sv = Sets.union(s1,s2); assertThat(sv.size()==4 && sv.contains("2") && sv.contains("3") && sv.contains("4") && sv.contains("1"),is(true)); }
呵呵,全是数学中的集合操作。当然Sets也可以和Predicate结合起来进行函数式编程。
更多功能等待你的发现。
0 0
- Guava collections -- Sets
- Guava ---- Sets
- Guava collections -- Immutable Collections
- Google Guava Collections
- Google Guava Collections
- Guava Collections使用介绍
- Guava collections -- FluentIterable
- Guava collections -- Lists
- Guava collections -- Maps
- Guava collections -- Multimaps
- Guava collections -- BiMap
- Guava collections -- Table
- Guava collections -- Range
- Guava collections -- Ordering
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- eclipse自动补全
- Linux的僵尸进程产生原因及解决方法
- Vue.js学习 Item6 -- Class 与 样式绑定
- select下拉列表的级联
- 263. Ugly Number
- Guava collections -- Sets
- java----IO流
- facebook工具xhprof的安装与使用-分析php执行性能
- Linux学习笔记六-shell
- 浅谈服务化
- IDEA 2016在创建spring项目时遇到的坑
- Windows客户端开发--使用WMI获取显卡详细信息(win32控制台程序)
- Stack介绍
- string类型