Guava collections -- Multimaps
来源:互联网 发布:杭州网络安防招聘 编辑:程序博客网 时间:2024/05/29 18:36
Guava全文介绍地址:Google Guava
这次主要介绍是的是com.google.common.collect.Multimaps.主要的作用是就与Guava Multimap相关的静态的实用方法。所以就不得不介绍一下com.google.common.collect.Multimap。
Multimap将键映射到值的集合,类似于地图,但在每个键可能与多个值有关。你可以想象把一个multimap想象成一个map,但是这个map可以包含非空的一个集合.
下面就是它的例子:
- a -> 1,2
- b -> 3
或者像map一样只是单个的key-value
- a -> 1
- b -> 2
- c -> 3 注意:
虽然第一例子解释像大多数多重映射是如何实现的,Multimap的API的设计是基于第二种形式。所以,使用上面所示的多重映射作为一个例子,它的size()3,不是2,它的value()的值集合是[1、2、3],而不是[[1,2],[3]]。对于第一个风格是更有用的那些时候,使用Multimap的asMap()视图(或者创建一个Map
1、Multimap的create()
@Test public void testArrayListMultiMap(){ ArrayListMultimap<String,String> multiMap = ArrayListMultimap.create(); multiMap.put("Foo","1"); multiMap.put("Foo","2"); multiMap.put("Foo","3"); List<String> expected = Lists.newArrayList("1","2","3"); assertEquals(multiMap.get("Foo"),expected); }
2、Multimap的的size()
@Test public void testArrayListMultiMapSize(){ ArrayListMultimap<String,String> multiMap = ArrayListMultimap.create(); multiMap.put("Foo","1"); multiMap.put("Foo","2"); multiMap.put("Foo","3"); multiMap.put("Bar","1"); multiMap.put("Bar","2"); multiMap.put("Bar","3"); Collection<String> expected = Lists.newArrayList("1","2","3","1","2","3"); assertThat(multiMap.size(),is(6)); assertArrayEquals(multiMap.values().toArray(), expected.toArray()); }
从上面的结果也就证明了上面的所说的MultiMap的value的存储方式。
3、Multimap的asMap()
@Test public void testArrayListMultiMapSizeAsMap(){ ArrayListMultimap<String,String> multiMap = ArrayListMultimap.create(); multiMap.put("Foo","1"); multiMap.put("Foo","2"); multiMap.put("Foo","3"); multiMap.put("Bar","1"); multiMap.put("Bar","2"); multiMap.put("Bar","3"); Map<String,Collection<String>> map = multiMap.asMap(); assertThat(map.size(),is(2)); map.get("Foo").remove("3"); assertThat(multiMap.size(), is(5)); multiMap.put("Baz", "1"); assertThat(map.size(), is(3)); map.get("Foo").add("4"); assertThat(multiMap.size(), is(7)); }
4、ArrayListMultimap多个一样的key值
@Test public void testArrayListMultimapSameKeyValue(){ ArrayListMultimap<String,String> multiMap = ArrayListMultimap.create(); multiMap.put("Bar","1"); multiMap.put("Bar","3"); multiMap.put("Bar","2"); multiMap.put("Bar","3"); multiMap.put("Bar","3"); List<String> expected = Lists.newArrayList("1","3","2","3","3"); assertEquals(multiMap.get("Bar"),expected); }
5、HashMultiMap多个一个的key值
@Test public void testHashMultiMapSameKeyValue(){ HashMultimap<String,String> multiMap = HashMultimap.create(); multiMap.put("Bar","1"); multiMap.put("Bar","2"); multiMap.put("Bar","3"); multiMap.put("Bar","3"); multiMap.put("Bar","3"); assertThat(multiMap.size(),is(3)); }
更多功能等待你的发现。
0 0
- Guava collections -- Multimaps
- Guava collections -- Immutable Collections
- MultiMaps
- Google Guava Collections
- Google Guava Collections
- Guava Collections使用介绍
- Guava collections -- FluentIterable
- Guava collections -- Lists
- Guava collections -- Sets
- Guava collections -- Maps
- Guava collections -- BiMap
- Guava collections -- Table
- Guava collections -- Range
- Guava collections -- Ordering
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- The java.util.concurrent Synchronizer Framework
- ThinkPHP之疑难杂症(六)——Mac终端设置定时任务
- 学IT看教程的学习网站
- 苹果常用操作以及快捷键,安装非app store 应用
- 关于css的布局
- Guava collections -- Multimaps
- block
- 以五子棋实例来提高程序模型思维的建设
- iOS 开发环境的搭建
- STM32 GPIO_2 8种工作方式
- C++ 函数后加const
- 切换eclipse package explorer字体大小
- iOS-C语言-C语言的介绍以及第一个例子HelloWorld
- const成员函数、const类对象、mutable数据成员