面试必备:ArrayMap源码解析

来源:互联网 发布:淘宝拍a发b教程 编辑:程序博客网 时间:2024/05/22 07:48
概括的说,ArrayMap 实现了implements Map<K, V>接口,所以它也是一个关联数组、哈希表。存储以key->value 结构形式的数据。它也是线程不安全的,允许key为null,value为null。它相比HashMap,空间效率更高。它的内部实现是基于两个数组。一个int[]数组,用于保存每个item的hashCode.一个Object[]数组,保存key/value键值对。容量是上一个数组的两倍。它可以避免在将数据插入Map中时额外的空间消耗(对比HashMap)。而且它扩容的更合适,扩容时只需要数组拷贝工作,不需要重建哈希表。和HashMap相比,它不仅有扩容功能,在删除时,如果集合剩余元素少于一定阈值,还有收缩(shrunk)功能。减少空间占用。图中存了三组元素,int[]数组长度为3,Object[]数组长度为6但是它不适合大容量的数据存储。存储大量数据时,它的性能将退化至少50%。比传统的HashMap时间效率低。因为其会对key使用二分法进行从小到大排序,在添加、删除、查找数据的时候都是先使用二分查找法得到相应的index,然后通过index来进行添加、查找、删除等操作。所以其是按照key的排序存储的。适用场景:数据量不大空间比时间重要需要使用Map在Android平台,相对来说,内存容量更宝贵。而且数据量不大。所以当需要使用key是Object类型的Map时,可以考虑使用ArrayMap来替换HashMap示例代码: Map<String, String> map = new ArrayMap<>(); map.put("1","1"); map.put(null,"2"); map.put("3",null); map.put("6",null); map.put("5",null); map.put("4",null); Log.d("TAG", "onCreate() called with: map = [" + map + "]");12345678输出: onCreate() called with: map = [{null=2, 1=1, 3=null, 4=null, 5=null, 6=null}]13 构造函数 //扩容默认的size, 4是相对效率较高的大小 private static final int BASE_SIZE = 4; //表示集合是不可变的 static final int[] EMPTY_IMMUTABLE_INTS = new int[0]; //是否利用System.identityHashCode(key) 获取唯一HashCode模式。 final boolean mIdentityHashCode; //保存hash值的数组 int[] mHashes; //保存key/value的数组。 Object[] mArray; //容量 int mSize; //创建一个空的ArrayMap,默认容量是0.当有Item被添加进来,会自动扩容 public ArrayMap() { this(0, false); } //创建一个指定容量的ArrayMap public ArrayMap(int capacity) { this(capacity, false); } //指定容量和identityHashCode public ArrayMap(int capacity, boolean identityHashCode) { mIdentityHashCode = identityHashCode; //数量< 0,构建一个不可变的ArrayMap if (capacity < 0) { mHashes = EMPTY_IMMUTABLE_INTS; mArray = EmptyArray.OBJECT; //数量= 0,构建空的mHashes mArray } else if (capacity == 0) { mHashes = EmptyArray.INT; mArray = EmptyArray.OBJECT; } else {//数量>0,分配空间初始化数组 allocArrays(capacity); } mSize = 0; } //扩容 private void allocArrays(final int size) { //数量< 0,构建一个不可变的ArrayMap if (mHashes == EMPTY_IMMUTABLE_INTS) { throw new UnsupportedOperationException("ArrayMap is immutable"); }//扩容数量是 8 if (size == (BASE_SIZE*2)) { synchronized (ArrayMap.class) { //查看之前是否有缓存的 容量为8的int[]数组和容量为16的object[]数组//如果有,复用给mArray mHashes if (mTwiceBaseCache != null) { final Object[] array = mTwiceBaseCache; mArray = array; mTwiceBaseCache = (Object[])array[0]; mHashes = (int[])array[1]; array[0] = array[1] = null; mTwiceBaseCacheSize--; if (DEBUG) Log.d(TAG, "Retrieving 2x cache " + mHashes + " now have " + mTwiceBaseCacheSize + " entries"); return; } } } else if (size == BASE_SIZE) {//扩容数量是4 synchronized (ArrayMap.class) { //查看之前是否有缓存的 容量为4的int[]数组和容量为8的object[]数组//如果有,复用给mArray mHashes if (mBaseCache != null) { final Object[] array = mBaseCache; mArray = array; mBaseCache = (Object[])array[0]; mHashes = (int[])array[1]; array[0] = array[1] = null; mBaseCacheSize--; if (DEBUG) Log.d(TAG, "Retrieving 1x cache " + mHashes + " now have " + mBaseCacheSize + " entries"); return; } } } //构建mHashes和mArray,mArray是mHashes的两倍。因为它既要存key还要存value。 mHashes = new int[size]; mArray = new Object[size<<1]; } //利用另一个map构建ArrayMap public ArrayMap(ArrayMap<K, V> map) { this(); if (map != null) { putAll(map); } } //批量put方法: public void putAll(ArrayMap<? extends K, ? extends V> array) { final int N = array.mSize; //确保空间足够存放 ensureCapacity(mSize + N); //如果当前是空集合, if (mSize == 0) { if (N > 0) {//则直接复制覆盖数组内容即可。 System.arraycopy(array.mHashes, 0, mHashes, 0, N); System.arraycopy(array.mArray, 0, mArray, 0, N<<1); mSize = N; } } else {//否则需要一个一个执行插入put操作 for (int i=0; i<N; i++) { put(array.keyAt(i), array.valueAt(i)); } } } //确保空间足够存放 minimumCapacity 个数据 public void ensureCapacity(int minimumCapacity) { //如果不够扩容 if (mHashes.length < minimumCapacity) { //暂存当前的hash array。后面复制需要 final int[] ohashes = mHashes; final Object[] oarray = mArray; //扩容空间(开头讲过这个函数) allocArrays(minimumCapacity); if (mSize > 0) {//如果原集合不为空,复制原数据到新数组中 System.arraycopy(ohashes, 0, mHashes, 0, mSize); System.arraycopy(oarray, 0, mArray, 0, mSize<<1); } //释放回收临时暂存数组空间 freeArrays(ohashes, oarray, mSize); } } //释放回收临时暂存数组空间 private static void freeArrays(final int[] hashes, final Object[] array, final int size) { //如果容量是8, 则将hashes 和array 缓存起来,以便下次使用 if (hashes.length == (BASE_SIZE*2)) { synchronized (ArrayMap.class) { if (mTwiceBaseCacheSize < CACHE_SIZE) { //0存,前一个缓存的cache array[0] = mTwiceBaseCache; //1 存 int[]数组 array[1] = hashes; //2+ 元素置空 以便GC for (int i=(size<<1)-1; i>=2; i--) { array[i] = null; } //更新缓存引用为array mTwiceBaseCache = array; //增加缓存过的Array的数量 mTwiceBaseCacheSize++; if (DEBUG) Log.d(TAG, "Storing 2x cache " + array +" now have " + mTwiceBaseCacheSize + " entries"); } }//相同逻辑,只不过缓存的是int[] 容量为4的数组} else if (hashes.length == BASE_SIZE) { synchronized (ArrayMap.class) { if (mBaseCacheSize < CACHE_SIZE) { array[0] = mBaseCache; array[1] = hashes; for (int i=(size<<1)-1; i>=2; i--) { array[i] = null; } mBaseCache = array; mBaseCacheSize++; if (DEBUG) Log.d(TAG, "Storing 1x cache " + array +" now have " + mBaseCacheSize + " entries"); } } } }123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162小结:*扩容时,会查看之前是否有缓存的 int[]数组和object[]数组 *如果有,复用给mArray mHashes4 增 、改4.1 单个增改 put(K key, V value) //如果key存在,则返回oldValue public V put(K key, V value) { //key的hash值 final int hash; //下标 int index; // 如果key为null,则hash值为0. if (key == null) { hash = 0; //寻找null的下标 index = indexOfNull(); } else { //根据mIdentityHashCode 取到 hash值 hash = mIdentityHashCode ? System.identityHashCode(key) : key.hashCode(); //根据hash值和key 找到合适的index index = indexOf(key, hash); } //如果index>=0,说明是替换(改)操作 if (index >= 0) { //只需要更新value 不需要更新key。因为key已经存在 index = (index<<1) + 1; //返回旧值 final V old = (V)mArray[index]; mArray[index] = value; return old; } //index<0,说明是插入操作。 对其取反,得到应该插入的下标 index = ~index; //如果需要扩容 if (mSize >= mHashes.length) { //如果容量大于8,则扩容一半。 //否则容量大于4,则扩容到8. //否则扩容到4 final int n = mSize >= (BASE_SIZE*2) ? (mSize+(mSize>>1)) : (mSize >= BASE_SIZE ? (BASE_SIZE*2) : BASE_SIZE); //临时数组 final int[] ohashes = mHashes; final Object[] oarray = mArray; //分配空间完成扩容 allocArrays(n); //复制临时数组中的数组进新数组 if (mHashes.length > 0) { if (DEBUG) Log.d(TAG, "put: copy 0-" + mSize + " to 0"); System.arraycopy(ohashes, 0, mHashes, 0, ohashes.length); System.arraycopy(oarray, 0, mArray, 0, oarray.length); } //释放临时数组空间 freeArrays(ohashes, oarray, mSize); } //如果index在中间,则需要移动数组,腾出中间的位置 if (index < mSize) { if (DEBUG) Log.d(TAG, "put: move " + index + "-" + (mSize-index) + " to " + (index+1)); System.arraycopy(mHashes, index, mHashes, index + 1, mSize - index); System.arraycopy(mArray, index << 1, mArray, (index + 1) << 1, (mSize - index) << 1); } //hash数组,就按照下标存哈希值 mHashes[index] = hash; //array数组,根据下标,乘以2存key,乘以2+1 存value mArray[index<<1] = key; mArray[(index<<1)+1] = value; mSize++;//修改size return null; } //返回key为null的 下标index int indexOfNull() { //N为当前集合sizefinal int N = mSize; //如果当前集合是空的,返回~0 if (N == 0) {// return ~0; } //根据hash值=0,通过二分查找,查找到目标index int index = ContainerHelpers.binarySearch(mHashes, N, 0); //如果index《0,则hash值=0之前没有存储过数据 if (index < 0) { return index; } //如果index>=0,说明该hash值,之前存储过数据,找到对应的key,比对key是否等于null。相等的话,返回index。说明要替换。//关于array中对应数据的位置,是index*2 = key ,index*2+1 = value. if (null == mArray[index<<1]) { return index; } //以下两个for循环是在出现hash冲突的情况下,找到正确的index的过程: //从index+1,遍历到数组末尾,找到hash值相等,且key相等的位置,返回 int end; for (end = index + 1; end < N && mHashes[end] == 0; end++) { if (null == mArray[end << 1]) return end; } //从index-1,遍历到数组头,找到hash值相等,且key相等的位置,返回 for (int i = index - 1; i >= 0 && mHashes[i] == 0; i--) { if (null == mArray[i << 1]) return i; } // key没有找到,返回一个负数。代表应该插入的位置 return ~end; } //根据key和key的hash值,返回index int indexOf(Object key, int hash) { //N为当前集合sizefinal int N = mSize; //如果当前集合是空的,返回~0 if (N == 0) { return ~0; } //根据hash值,通过二分查找,查找到目标index int index = ContainerHelpers.binarySearch(mHashes, N, hash); //如果index《0,说明该hash值之前没有存储过数据 if (index < 0) { return index; } //如果index>=0,说明该hash值,之前存储过数据,找到对应的key,比对key是否相等。相等的话,返回index。说明要替换。 if (key.equals(mArray[index<<1])) { return index; } //以下两个for循环是在出现hash冲突的情况下,找到正确的index的过程: //从index+1,遍历到数组末尾,找到hash值相等,且key相等的位置,返回 int end; for (end = index + 1; end < N && mHashes[end] == hash; end++) { if (key.equals(mArray[end << 1])) return end; } //从index-1,遍历到数组头,找到hash值相等,且key相等的位置,返回 for (int i = index - 1; i >= 0 && mHashes[i] == hash; i--) { if (key.equals(mArray[i << 1])) return i; } // key没有找到,返回一个负数。代表应该插入的位置 return ~end; }1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281294.2 批量增 putAll(Map //批量增,接受更为广泛的Map参数 public void putAll(Map<? extends K, ? extends V> map) { //确保空间容量足够 ensureCapacity(mSize + map.size()); for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) { //分别调用单个增方法 add put(entry.getKey(), entry.getValue()); } }123456789小结:*增的流程:1 先根据key得到hash值,2 根据hash值得到index 3 根据index正负,得知是插入还是替换 4 如果是替换直接替换值即可 5 如果是插入,则先判断是否需要扩容,并进行扩容 6 挪动数组位置,插入元素(类似ArrayList)插入允许key为null,value为null。每次插入时,根据key的哈希值,利用二分查找,去寻找key在int[] mHashes数组中的下标位置。如果出现了hash冲突,则从需要从目标点向两头遍历,找到正确的index。如果index>=0,说明之前已经存在该key,需要替换(改)。如果index<0,说明没有找到。(也是二分法特性)对index去反,可以得到这个index应该插入的位置。根据key的hash值在mHashs中的index,如何得到key、value在mArray中的下标位置呢?key的位置是index*2,value的位置是index*2+1,也就是说mArray是利用连续的两位空间去存放key、value。根据hash值的index计算,key、value的index也利用了位运算。index<<1 和 (index<<1)+15 删5.1 单个删 //如果对应key有元素存在,返回value。否则返回null public V remove(Object key) { //根据key,找到下标 final int index = indexOfKey(key); if (index >= 0) { //如果index>=0,说明key有对应的元素存在,则去根据下标删除 return removeAt(index); } //否则返回null return null; }1234567891011 //根据下标删除元素 public V removeAt(int index) { //根据index,得到value final Object old = mArray[(index << 1) + 1]; //如果之前的集合长度小于等于1,则执行过删除操作后,集合现在就是空的了 if (mSize <= 1) { // Now empty. if (DEBUG) Log.d(TAG, "remove: shrink from " + mHashes.length + " to 0"); //释放回收空间 freeArrays(mHashes, mArray, mSize); //置空 mHashes = EmptyArray.INT; mArray = EmptyArray.OBJECT; mSize = 0; } else {//根据元素数量和集合占用的空间情况,判断是否要执行收缩操作 //如果 mHashes长度大于8,且 集合长度 小于当前空间的 1/3,则执行一个 shrunk,收缩操作,避免空间的浪费 if (mHashes.length > (BASE_SIZE*2) && mSize < mHashes.length/3) { // Shrunk enough to reduce size of arrays. We dont allow it to // shrink smaller than (BASE_SIZE*2) to avoid flapping between // that and BASE_SIZE. //如果当前集合长度大于8,则n为当前集合长度的1.5倍。否则n为8. //n 为收缩后的 mHashes长度 final int n = mSize > (BASE_SIZE*2) ? (mSize + (mSize>>1)) : (BASE_SIZE*2); if (DEBUG) Log.d(TAG, "remove: shrink from " + mHashes.length + " to " + n); //分配新的更小的空间(收缩操作) final int[] ohashes = mHashes; final Object[] oarray = mArray; allocArrays(n); //删掉一个元素,所以修改集合元素数量 mSize--; //因为执行了收缩操作,所以要将老数据复制到新数组中。 if (index > 0) { if (DEBUG) Log.d(TAG, "remove: copy from 0-" + index + " to 0"); System.arraycopy(ohashes, 0, mHashes, 0, index); System.arraycopy(oarray, 0, mArray, 0, index << 1); } //在复制的过程中,排除不复制当前要删除的元素即可。 if (index < mSize) { if (DEBUG) Log.d(TAG, "remove: copy from " + (index+1) + "-" + mSize + " to " + index); System.arraycopy(ohashes, index + 1, mHashes, index, mSize - index); System.arraycopy(oarray, (index + 1) << 1, mArray, index << 1, (mSize - index) << 1); } } else {//不需要收缩 //修改集合长度 mSize--; //类似ArrayList,用复制操作去覆盖元素达到删除的目的。 if (index < mSize) { if (DEBUG) Log.d(TAG, "remove: move " + (index+1) + "-" + mSize +" to " + index); System.arraycopy(mHashes, index + 1, mHashes, index, mSize - index); System.arraycopy(mArray, (index + 1) << 1, mArray, index << 1, (mSize - index) << 1); } //记得置空,以防内存泄漏 mArray[mSize << 1] = null; mArray[(mSize << 1) + 1] = null; } } //返回删除的值 return (V)old; }123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263645.2 批量删除 //从ArrayMap中,删除Collection集合中,所有出现的key。 //返回值代表是否成功删除元素 public boolean removeAll(Collection<?> collection) { return MapCollections.removeAllHelper(this, collection); } //MapCollections.removeAllHelper(this, collection); //遍历Collection,调用Map.remove(key)去删除元素; public static <K, V> boolean removeAllHelper(Map<K, V> map, Collection<?> collection) { int oldSize = map.size(); Iterator<?> it = collection.iterator(); while (it.hasNext()) { map.remove(it.next()); } //如果元素不等,说明成功删除元素 return oldSize != map.size(); }12345678910111213141516根据元素数量和集合占用的空间情况,判断是否要执行收缩操作类似ArrayList,用复制操作去覆盖元素达到删除的目的。6 查当你想获取某个value的时候,ArrayMap会计算输入key转换过后的hash值,然后对hash数组使用二分查找法寻找到对应的index,然后我们可以通过这个index在另外一个数组中直接访问到需要的键值对。如果在第二个数组键值对中的key和前面输入的查询key不一致,那么就认为是发生了碰撞冲突。为了解决这个问题,我们会以该key为中心点,分别上下展开,逐个去对比查找,直到找到匹配的值。如下图所示:随着数组中的对象越来越多,查找访问单个对象的花费也会跟着增长,这是在内存占用与访问时间之间做权衡交换。6.1 单个查 public V get(Object key) { //根据key去得到index final int index = indexOfKey(key); //根据 index*2+1 得到value return index >= 0 ? (V)mArray[(index<<1)+1] : null; } public int indexOfKey(Object key) { //判断key是否是null,并去查询key对应的index return key == null ? indexOfNull() : indexOf(key, mIdentityHashCode ? System.identityHashCode(key) : key.hashCode()); }
原创粉丝点击