Java提供了HashMap,但是HashMap对于手机端而言,对内存的占用太大,所以Android提供了SparseArray和ArrayMap。二者都是基于二分查找,所以数据量大的时候,最坏效率会比HashMap慢很多。因此建议数据量在千以内比较合适。

一、SparseArray

  SparseArray对应的key只能是int类型,它不会对key进行装箱操作。它使用了两个数组,一个保存key,一个保存value。

  SparseArray使用二分查找来找到key对应的插入位置。所以要保证mKeys数组有序。

  remove的时候不会立刻重新清理删除掉的数据,而是将对一个的数据标记为DELETE(一个Object对象)。在必要的环节调用gc清理标记为DELETE的空间。

二、ArrayMap

  重点介绍一下ArrayMap。

  首先从ArrayMap的四个数组说起。

    • mHashes,用于保存key对应的hashCode;
    • mArray,用于保存键值对(key,value),其结构为[key1,value1,key2,value2,key3,value3,......];
    • mBaseCache,缓存,如果ArrayMap的数据量从4,增加到8,用该数组保存之前使用的mHashes和mArray,这样如果数据量再变回4的时候,可以再次使用之前的数组,不需要再次申请空间,这样节省了一定的时间;
    • mTwiceBaseCache,与mBaseCache对应,不过触发的条件是数据量从8增长到12。

  

  上面提到的数据量有8增长到12,为什么不是16?这也算是ArrayMap的一个优化的点,它不是每次增长1倍,而是使用了如下方法(mSize+(mSize>>1)),即每次增加1/2。

  有了上面的说明,读懂代码就容易多了。

  1、很多地方用到的indexOf

    这里使用了二分查找来查找对应的index

int indexOf(Object key, int hash) {
final int N = mSize; // Important fast case: if nothing is in here, nothing to look for.
//数组为空,直接返回
if (N == 0) {
return ~0;
} //二分查找,不细说了
int index = ContainerHelpers.binarySearch(mHashes, N, hash); // If the hash code wasn't found, then we have no entry for this key.
//没找到hashCode,返回index,一个负数
if (index < 0) {
return index;
} // If the key at the returned index matches, that's what we want.
//对比key值,相同则返回index
if (key.equals(mArray[index<<1])) {
return index;
} // Search for a matching key after the index.
//如果返回的index对应的key值,与传入的key值不等,则可能对应的key在index后面
int end;
for (end = index + 1; end < N && mHashes[end] == hash; end++) {
if (key.equals(mArray[end << 1])) return end;
} // Search for a matching key before the index.
//接上句,后面没有,那一定在前面。
for (int i = index - 1; i >= 0 && mHashes[i] == hash; i--) {
if (key.equals(mArray[i << 1])) return i;
} // Key not found -- return negative value indicating where a
// new entry for this key should go. We use the end of the
// hash chain to reduce the number of array entries that will
// need to be copied when inserting.
//毛都没找到,那肯定是没有了,返回个负数
return ~end;
}

  2、看一下put方法

public V put(K key, V value) {
final int hash;
int index;
//key是空,则通过indexOfNull查找对应的index;如果不为空,通过indexOf查找对应的index
if (key == null) {
hash = 0;
index = indexOfNull();
} else {
hash = key.hashCode();
index = indexOf(key, hash);
} //index大于或等于0,一定是之前put过相同的key,直接替换对应的value。因为mArray中不只保存了value,还保存了key。
//其结构为[key1,value1,key2,value2,key3,value3,......]
//所以,需要将index乘2对应key,index乘2再加1对应value
if (index >= 0) {
index = (index<<1) + 1;
final V old = (V)mArray[index];
mArray[index] = value;
return old;
} //取正数
index = ~index;
//mSize的大小,即已经保存的数据量与mHashes的长度相同了,需要扩容啦
if (mSize >= mHashes.length) {
//扩容后的大小,有以下几个档位,BASE_SIZE(4),BASE_SIZE的2倍(8),mSize+(mSize>>1)(比之前的数据量扩容1/2)
final int n = mSize >= (BASE_SIZE*2) ? (mSize+(mSize>>1))
: (mSize >= BASE_SIZE ? (BASE_SIZE*2) : BASE_SIZE); if (DEBUG) Log.d(TAG, "put: grow from " + mHashes.length + " to " + n); 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);
} //看看被废弃的数组是否还有利用价值
//如果被废弃的数组的数据量为4或8,说明可能利用价值,以后用到的时候可以直接用。
//如果被废弃的数据量太大,扔了算了,要不太占内存。如果浪费内存了,还费这么大劲,加了类干啥。
freeArrays(ohashes, oarray, mSize);
} //这次put的key对应的hashcode排序没有排在最后(index没有指示到数组结尾),因此需要移动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);
} //把数据保存到数组中。看到了吧,key和value都在mArray中;hashCode放到mHashes
mHashes[index] = hash;
mArray[index<<1] = key;
mArray[(index<<1)+1] = value;
mSize++;
return null;
}

  

  3、remove方法

  remove方法在某种条件下,会重新分配内存,保证分配给ArrayMap的内存在合理区间,减少对内存的占用。但是如果每次remove都重新分配空间,会浪费大量的时间。因此在此处,Android使用的是用空间换时间的方式,以避免效率低下。无论从任何角度,频繁的分配回收内存一定会耗费时间的。

  remove最终使用的是removeAt方法,此处只说明removeAt

    /**
* Remove the key/value mapping at the given index.
* @param index The desired index, must be between 0 and {@link #size()}-1.
* @return Returns the value that was stored at this index.
*/
public V removeAt(int index) {
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");
//put中已有说明
freeArrays(mHashes, mArray, mSize);
mHashes = EmptyArray.INT;
mArray = EmptyArray.OBJECT;
mSize = 0;
} else {
//如果当初申请的数组最大容纳数据个数大于BASE_SIZE的2倍(8),并且现在存储的数据量只用了申请数量的1/3,
//则需要重新分配空间,已减少对内存的占用
if (mHashes.length > (BASE_SIZE*2) && mSize < mHashes.length/3) {
// Shrunk enough to reduce size of arrays. We don't allow it to
// shrink smaller than (BASE_SIZE*2) to avoid flapping between
// that and BASE_SIZE.
//新数组的大小
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--;
//index之前的数据拷贝到新数组中
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);
}
//将index之后的数据拷贝到新数组中,和(index>0)的分支结合,就将index位置的数据删除了
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--;
//将index后的数据向前移位
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;
}

  4、freeArrays

    put中有说明,这里就不进行概述了,直接上代码,印证上面的说法。

  private static void freeArrays(final int[] hashes, final Object[] array, final int size) {
//已经废弃的数组个数为BASE_SIZE的2倍(8),则用mTwiceBaseCache保存废弃的数组;
//如果个数为BASE_SIZE(4),则用mBaseCache保存废弃的数组
if (hashes.length == (BASE_SIZE*2)) {
synchronized (ArrayMap.class) {
if (mTwiceBaseCacheSize < CACHE_SIZE) {
//array为刚刚废弃的数组,mTwiceBaseCache如果有内容,则放入array[0]位置,
//在allocArrays中会从array[0]取出,放回mTwiceBaseCache
array[0] = mTwiceBaseCache;
//array[1]存放hash数组。因为array中每个元素都是Object对象,所以每个元素都可以存放数组
array[1] = hashes;
//清除index为2和之后的数据
for (int i=(size<<1)-1; i>=2; i--) {
array[i] = null;
}
mTwiceBaseCache = array;
mTwiceBaseCacheSize++;
if (DEBUG) Log.d(TAG, "Storing 2x cache " + array
+ " now have " + mTwiceBaseCacheSize + " entries");
}
}
} 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");
}
}
}
}

  5、allocArrays

    算了,感觉没啥好说的,看懂了freeArrays,allocArrays自然就理解了。

    总体来说,通过新数组的个数产生3个分支,个数为BASE_SIZE(4),从mBaseCache取之前废弃的数组;BASE_SIZE的2倍(8),从mTwiceBaseCache取之前废弃的数组;其他,之前废弃的数组没有存储,因为太耗费内存,这种情况下,重新分配内存。

  6、clear和erase

    clear清空数组,如果再向数组中添加元素,需要重新申请空间;erase清除数组中的数组,空间还在。

  7、get

    主要的逻辑都在indexOf中了,剩下的代码不需要分析了,看了的都说懂(窃笑)。

ArrayMap代码分析的更多相关文章

  1. Android代码分析工具lint学习

    1 lint简介 1.1 概述 lint是随Android SDK自带的一个静态代码分析工具.它用来对Android工程的源文件进行检查,找出在正确性.安全.性能.可使用性.可访问性及国际化等方面可能 ...

  2. pmd静态代码分析

    在正式进入测试之前,进行一定的静态代码分析及code review对代码质量及系统提高是有帮助的,以上为数据证明 Pmd 它是一个基于静态规则集的Java源码分析器,它可以识别出潜在的如下问题:– 可 ...

  3. [Asp.net 5] DependencyInjection项目代码分析-目录

    微软DI文章系列如下所示: [Asp.net 5] DependencyInjection项目代码分析 [Asp.net 5] DependencyInjection项目代码分析2-Autofac [ ...

  4. [Asp.net 5] DependencyInjection项目代码分析4-微软的实现(5)(IEnumerable<>补充)

    Asp.net 5的依赖注入注入系列可以参考链接: [Asp.net 5] DependencyInjection项目代码分析-目录 我们在之前讲微软的实现时,对于OpenIEnumerableSer ...

  5. 完整全面的Java资源库(包括构建、操作、代码分析、编译器、数据库、社区等等)

    构建 这里搜集了用来构建应用程序的工具. Apache Maven:Maven使用声明进行构建并进行依赖管理,偏向于使用约定而不是配置进行构建.Maven优于Apache Ant.后者采用了一种过程化 ...

  6. STM32启动代码分析 IAR 比较好

    stm32启动代码分析 (2012-06-12 09:43:31) 转载▼     最近开始使用ST的stm32w108芯片(也是一款zigbee芯片).开始看他的启动代码看的晕晕呼呼呼的. 还好在c ...

  7. 常用 Java 静态代码分析工具的分析与比较

    常用 Java 静态代码分析工具的分析与比较 简介: 本文首先介绍了静态代码分析的基 本概念及主要技术,随后分别介绍了现有 4 种主流 Java 静态代码分析工具 (Checkstyle,FindBu ...

  8. SonarQube-5.6.3 代码分析平台搭建使用

    python代码分析 官网主页: http://docs.sonarqube.org/display/PLUG/Python+Plugin Windows下安装使用: 快速使用: 1.下载jdk ht ...

  9. angular代码分析之异常日志设计

    angular代码分析之异常日志设计 错误异常是面向对象开发中的记录提示程序执行问题的一种重要机制,在程序执行发生问题的条件下,异常会在中断程序执行,同时会沿着代码的执行路径一步一步的向上抛出异常,最 ...

随机推荐

  1. sql server 压缩备份数据库

    转自 https://www.cnblogs.com/527289276qq/p/6273291.html 今天需要把一个省外项目的数据库从服务器上备份.拷贝到本机(跨地域传输数据库备份文件). 连上 ...

  2. esp8266(0) AT指令

    http://www.shaoguoji.cn/2017/01/15/ESP8266-usage/ 注意事项 使用WiFi模块的一些细节及注意事项 模块使用3.3V供电,一定注意电源的稳定,一些USB ...

  3. Linux监控命令整理(top,free,vmstat,iostat,mpstat,sar,netstat)

    1.1 top 1.1.1 命令说明 Top 命令能够实时监控系统的运行状态,并且可以按照cpu.内存和执行时间进行排序 1.1.2 参数说明 命令行启动参数: 用法: top -hv | -bcis ...

  4. FreeRTOS任务函数

    FreeRTOS的任务函数原型如下:void ATaskFunction(void *pvParameters); void ATaskFunction(*pvParameters){;; //函数可 ...

  5. Android使用属性动画ValueAnimator动态改变SurfaceView的背景颜色

    以下是主要代码,难点和疑问点都写在注释中: /** * 开始背景动画(此处为属性动画) */ private void startBackgroundAnimator(){ /* *参数解释: *ta ...

  6. kubernete 数据库 etcd

    etcdctl --cert-file /etc/ssl/etcd/ssl/member-pserver78.pem --key-file /etc/ssl/etcd/ssl/member-pserv ...

  7. jdk_1_8_1

    JAVA_HOME=/usr/local/java/jdk1.8.0_181 PATH=$JAVA_HOME/bin:$PATH JAVA_BINDIR=/usr/local/java/jdk1.8. ...

  8. wifidog源码分析 - wifidog原理

    wifidog是一个用于配合认证服务器实现无线网页认证功能的程序,常见的情景就是使用于公共场合的无线wifi接入点,首先移动设备会连接公共wifi接入点,之后会弹出网页要求输入用户名密码,认证过后才能 ...

  9. [01] 浅谈RESTful风格的API

    1.什么是RESTful风格的API REST,即Representational State Transfer,可以理解为"(资源的)表现层状态转化". 在网络上,我们通过浏览器 ...

  10. Github.Git

    Github介绍:http://www.yangzhiping.com/tech/github.html Git:http://res.crossincode.com/wechat/git.html