Guava之ArrayListMultimap
来源:互联网 发布:李喆工笔画网络班 编辑:程序博客网 时间:2024/06/15 07:49
Class
ArrayListMultimap
All Implemented Interfaces
ListMultimap
简介
Implementation of Multimap that uses an ArrayList to store the values for a given key. A HashMap associates each key with an ArrayList of values.When iterating through the collections supplied by this class, the ordering of values for a given key agrees with the order in which the values were added.This multimap allows duplicate key-value pairs. After adding a new key-value pair equal to an existing key-value pair, the ArrayListMultimap will contain entries for both the new value and the old value.Keys and values may be null. All optional multimap methods are supported, and all returned views are modifiable.The lists returned by get(K), removeAll(java.lang.Object), and replaceValues(K, java.lang.Iterable
简言之
ArrayListMultimap底层可以认为是Map<K, Collection<V>>
, 且key可以重复
常用方法
boolean put(K key, V value)
说明: Stores a key-value pair in the multimap.
List<V> get(K key)
说明: Returns a view collection of the values associated with key in this multimap, if any.
Map<K,Collection<V>> asMap()
说明: Returns a view of this multimap as a Map from each distinct key to the nonempty collection of that key’s associated values.
Collection<Map.Entry<K,V>> entries()
说明: Returns a view collection of all key-value pairs contained in this multimap, as Map.Entry instances.
boolean containsEntry(Object key, Object value)
说明: Returns true if this multimap contains at least one key-value pair with the key key and the value value.
boolean containsKey(Object key)
说明: Returns true if this multimap contains at least one key-value pair with the key key.
boolean containsValue(Object value)
说明: Returns true if this multimap contains at least one key-value pair with the value value.
int size()
说明: Returns the number of key-value pairs in this multimap.
Multiset<K> keys()
说明: Returns a view collection containing the key from each key-value pair in this multimap, without collapsing duplicates.
Collection<V> values()
说明: Returns a view collection containing the value from each key-value pair contained in this multimap, without collapsing duplicates (so values().size() == size()).
使用场景及示例
//传统的场景: Map<String,List<MyClass>> map = new HashMap<String,List<MyClass>>(); #缺点:向map里面添加元素不太方便,需要这样实现 void putMyObject(String key, Object value) { List<Object> myClassList = myClassListMap.get(key); if(myClassList == null) { myClassList = new ArrayList<object>(); myClassListMap.put(key,myClassList); } myClassList.add(value); } //补充: HashMap的key和value皆可为null,HashTable的key和value皆不可为null. 二者的key皆不可重复,若重复,后添加的会覆盖之前的.//上面传统的场景,可以使用ArrayListMultimap Multimap<String, String> multimap = ArrayListMultimap.create();multimap.put("fruit", "bannana");multimap.put("fruit", "apple");//key可以重复multimap.put("fruit", "apple");//value可以重复,不会覆盖之前的multimap.put("fruit", "peach");multimap.put("fish","crucian");//欧洲鲫鱼multimap.put("fish","carp");//鲤鱼System.err.println(multimap.size());//6Collection<String> fruits = multimap.get("fruit");System.err.println(fruits);//[bannana, apple, apple, peach]for (String s : multimap.values()) { System.err.print(s + " , ");//bannana , apple , apple , peach , crucian , carp ,}multimap.remove("fruit","apple");System.err.println(fruits);//[bannana, apple, peach] 注意:这里只remove了一个apple,因此还有一个applemultimap.removeAll("fruit");System.err.println(fruits);//[]#TODO: 如果将multimap 直接返回前端, 返回的数据是 {"empty":false} , 是否可以返回给前端,Lifu不清楚//get(key) 返回的是collection,如果希望返回的是list,可以选择ListMultimap来接收create()的返回值 ListMultimap<String, String> listMultimap = ArrayListMultimap.create(); listMultimap.put("fruit", "bannana"); listMultimap.put("fruit", "apple"); listMultimap.put("fruit", "peach"); listMultimap.put("fish","crucian");//欧洲鲫鱼 listMultimap.put("fish","carp");//鲤鱼 List<String> fruits = listMultimap.get("fruit"); System.err.println(fruits);//[bannana, apple, peach]//对比 HashMultimap Multimap<String,String> multimap= HashMultimap.create(); multimap.put("fruit", "bannana"); multimap.put("fruit", "apple"); multimap.put("fruit", "apple"); System.err.println(multimap.size());//2 System.err.println(multimap.get("fruit"));//[apple, bannana] 注意: 这里只有一个apple
- Guava学习之ArrayListMultimap
- Guava之ArrayListMultimap
- Google Guava--ArrayListMultimap方法使用
- guava学习之——guava简介
- Guava学习之Multimap
- Guava学习之BiMap
- Guava学习之Multisets
- Guava学习之旅
- Guava学习之Range
- Guava学习之RangeSet
- Guava学习之RangeMap
- Guava学习之Preconditions
- Guava之eventbus
- Guava 之eventbus 二
- Guava学习之Splitter
- Guava学习之Lists
- Guava学习之Iterators
- Guava学习之AbstractMapBasedMultimap
- 20171215.05
- 不可不知的android开发冷知识8
- 购物车之Recyclerview列表
- SharedPreference的存值取值
- angular js 结算总价
- Guava之ArrayListMultimap
- 文件权限
- tensorflow-offline 离线安装
- 初学C#,对委托的初认识
- EL表达式&Ognl框架
- maven做ssm连接数据库的必备格式
- JS之数组
- hibernate与mybatis比较
- 使用ECharts构建饼状图的示例