今天看了下google开源工具类的源码,主要看了下Multimap的源码,写一下自己阅读中的一些总结。
在JDK中的Map,一个键对应一个值,值可以重复,键不能重复,相同的键会导致值得覆盖,这是最基本的Map的功能。Multimap实现的是一个键对应多个值,通过相同键得到值结果是一个Collection。
引用下源码中对Multimap的描述:
写道
A collection that maps keys to values, similar to {@link Map}, but in which each key may be associated with <i>multiple</i> values. You can visualize the contents of a multimap either as a map from keys to <i>nonempty</i> collections of values
下面是演示一个简单的小示例来说明这个简单的功能:
package com.google.test; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Multimap; public class GoogleTest { public static void main(String[] args) { Multimap<String, String> loverName = ArrayListMultimap.create(); loverName.put("lover", "翠花"); loverName.put("lover", "凤姐"); System.out.println(loverName.get("lover")); } } 结果:[翠花, 凤姐]
Multimap是一个接口,它的实现类有好几种:
①ArrayListMultimap:它是用一个ArrayList来存放给定的键对应的值集合。
ArrayListMultimap中的部分源码: public final class ArrayListMultimap<K, V> extends AbstractListMultimap<K, V> { // Default from ArrayList private static final int DEFAULT_VALUES_PER_KEY = 3; @VisibleForTesting transient int expectedValuesPerKey; /** * Creates a new, empty {@code ArrayListMultimap} with the default initial * capacities. */ public static <K, V> ArrayListMultimap<K, V> create() { return new ArrayListMultimap<K, V>(); } private ArrayListMultimap() { super(new HashMap<K, Collection<V>>()); expectedValuesPerKey = DEFAULT_VALUES_PER_KEY; } ...(省略) } 父类AbstractKistMultimap部分源码: abstract class AbstractListMultimap<K, V> extends AbstractMapBasedMultimap<K, V> implements ListMultimap<K, V> { /** * Creates a new multimap that uses the provided map. * * @param map place to store the mapping from each key to its corresponding * values */ protected AbstractListMultimap(Map<K, Collection<V>> map) { super(map); } ...(省略) } 其实最终也就是用AbstractMapBasedMultimap类中的 private transient Map<K, Collection<V>> map去存储的
看到上面的源码就解释了一键对多值的原理了。
知道上面ArrayListMultimap的存储原理, 也就明白各个方法的实现原理了
put 方法的实现源码 public boolean put(@Nullable K key, @Nullable V value) { return get(key).add(value); } 通过get得到Map对应value集合,然后add值到集合中。 get方法的实现源码 public Collection<V> get(@Nullable K key) { Collection<V> collection = map.get(key); if (collection == null) { collection = createCollection(key); } return wrapCollection(key, collection); }
相关推荐
Guava is a set of core Java libraries from Google that includes new collection types (such as multimap and multiset), immutable collections, a graph library, and utilities for concurrency, I/O, ...
谷歌guava的jar包和源码,很值得收藏。直接通过exlipse的build path,导入jar包和源码,可供开发和学习
├─Google Guava 第23讲-Guava EventBus源码剖析以及优缺点总结.wmv ├─Google Guava 第24讲-Guava之Monitor使用讲解.wmv ├─Google Guava 第25讲-Guava之RateLimiter在漏桶限流算法中的使用.wmv ├─Google ...
Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库,例如:集合 [collections] 、缓存 [caching] 、原生类型支持 [primitives support] 、并发库 [concurrency libraries] 、通用注解 [common annotations] ...
Guava是一种基于开源的Java库,其中包含谷歌正在由他们很多项目使用的很多核心库。这个库是为了方便编码,并减少编码错误。这个库提供用于集合,缓存,支持原语,并发性,常见注解,字符串处理,I/O和验证的实用方法...
guava学习体系知识点的学习,归纳出来了知识点。根据知识点去学习可以快熟了解guava
使用google guava 实现定时缓存功能
Google Guava 官方教程 - 详情:https://blog.csdn.net/Dream_Weave/article/details/86252213
Getting Started with Google Guava, guava的技术参考文档
google guava框架,包括Getting Started with Google Guava、google cache、base、io等介绍
guava 学习guava 最佳实践guava 基础工具guava 集合guava 源码学习ComparisonChain 源码学习
Google Guava 官方教程 - v1.1
Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, 等等. 这些高质量的 API 可以使你...
guava1.9 的jar包和源码包
汪文君Google Guava 代码。Guava之函数式接口、Guava EventBus源码剖析以及优缺点总结
Getting Started with Google Guava code
Guava的EventBus源码学习,基于Guava的18.0版本 分析者: ###1. 背景简介 EventBus是google的一个Java工具包其中的一个工具类,类似的有多个版本其中包括移植到Android端的 和改良的,功能基本都是一样的。此处主要...
Getting Started with Google Guava.pdf -Bill Bejeck
此文档为Guava中最流行和最强大的功能,提供更具可读性和解释性的说明
Google Guava 官方教程 v1.1 2018-07-22 https://github.com/tianbian224/GuavaLearning/blob/master/Google%20Guava%20%E5%AE%98%E6%96%B9%E6%95%99%E7%A8%8B%20-%20v1.1.pdf