可能在Android 中使用HashMap 的时候看到过提示。

HashMap<Integer,Bitmap> mp = new HashMap<Integer,Bitmap>();

提示:Use new SparseArray<Bitmap>(...) instead for better performance意思是,使用 SparseArray 将获得更好的性能

(注:这个提示我再eclipse 中见过,而在studio 中并没有看到过这种提示)

那么SparseArray这个类是干嘛使得,有什么长处,特性呢?

ok。我们从以下几点介绍下SparseArray 这个类。

1.武功秘籍之SparseArray(SparseArray文档介绍)

SparseArrays map integers to Objects. Unlike a normal array of Objects, there can be gaps in the indices. It is intended to be more memory efficient than using a HashMap to map Integers to Objects, both because it avoids auto-boxing keys and its data structure doesn’t rely on an extra entry object for each mapping.

(译文+个人理解)

SparseArrays不同于普通的对象数组。there can be gaps in the indices.(指针中能够存在空白?注:这句我不太理解,不知道怎样翻译,是说SparseArrays 是不连续存储?)。它的目的是比使用从整数映射对象的HashMap(简单来说就是 HashMap<Integer,Object>)有更有效的使用内存。

同一时候也避免auto-boxing(自己主动装箱)(注:auto-boxing(自己主动装箱):将原始类型封装为对象类型。比方把int类型封装成Integer类型。)和数据结构对每条映射不依赖额外的Entry 对象(注:这里是和HashMap做的对照。在HashMap有须要引入Entry<K,V>,而SparseArrays比較简单,里面是两个一维数组 int[] mKeys; 和 Object[] mValues)

Note that this container keeps its mappings in an array data structure, using a binary search to find keys. The implementation is not intended to be appropriate for data structures that may contain large numbers of items. It is generally slower than a traditional HashMap, since lookups require a binary search and adds and removes require inserting and deleting entries in the array. For containers holding up to hundreds of items, the performance difference is not significant, less than 50%.

注意,这个容器在数组数据结构中维持一个映射关系。使用二分查找法来找到key.它的目标不是为了适应数据结构中包括大量数据的情况。

通常情况下要比传统的HashMap慢,由于查找是用二分查找法搜索,加入和删除须要对数组进行加入和删除。

对于有几百条的数据的容器。性能差异不大,不超过50%(注:这里我理解的是, SparseArrays 的优势更体如今小数量上)

To help with performance, the container includes an optimization when removing keys: instead of compacting its array immediately, it leaves the removed entry marked as deleted. The entry can then be re-used for the same key, or compacted later in a single garbage collection step of all removed entries. This garbage collection will need to be performed at any time the array needs to be grown or the the map size or entry values are retrieved.

为了提高性能,该容器提供了一个优化:当删除key键时。不是立刻删除这一项,而是留下须要删除的选项给一个删除的标记。

该条目能够被又一次用于同样的key,或者被单个垃圾收集器逐步删除完所有的条目后压缩。在不论什么时候。当数组须要增长(注:这里我理解为 put、append之类的操作)或者Map 的长度、entry的value须要被检索的时候,该垃圾收集器就会运行(注:这里能够从代码中发现。google在相应的方法中调用 gc() 方法)。

It is possible to iterate over the items in this container using

* {@link #keyAt(int)} and {@link #valueAt(int)}. Iterating over the keys using

* keyAt(int) with ascending values of the index will return the

* keys in ascending order, or the values corresponding to the keys in ascending

* order in the case of valueAt(int).

在此使用此容器时,能够迭代遍历当中的item.. keyAt(int) 返回升序下的key .

valueAt(int) 返回升序状态下。key 相应的value值。

2.SparseArray,HashMap 不服?SOLO

2.1 key,value 类型的比較

HashMap                   key:随意类型         value:随意类型
SparseArray key:Integer value:随意类型
SparseBooleanArray key:Integer value:Boolean类型。
SparseIntArray key:Integer value:Integer类型。
LongSparseArray key:Long value:随意类型 依据需求的不同,找到合适的方法才是上上策。

2.2 内部存储的比較

SparseArray  :int[] mKeys 和 Object[] mValues 来存储 key 和 value
HashMap :内部存储须要用到 `Entry<K,V>`

2.3 运行速度比較

这里不打算做代码的介绍了。这篇文章里有比較具体的介绍,这里我仅仅打算总结下。(http://www.open-open.com/lib/view/open1402906434918.html

  1. 创建数据

    以10000条数据为例。HashMap用去约13.2M内存,SparseArray共用去 8.626M内存。

  2. 数据插入

    在正序插入数据时候,SparseArray比HashMap要快一些;HashMap无论是倒序还是正序开销差点儿是一样的。可是SparseArray的倒序插入要比正序插入要慢很多,为什么呢?

    原因:SparseArray在检索数据的时候使用的是二分查找,所以每次插入新数据的时候SparseArray都须要又一次排序,所以逆序是最差情况。

  3. 数据检索

    SparseArray中存在须要检索的下标时。SparseArray的性能略胜一筹可是当检索的下标比較离散时,SparseArray须要使用多次二分检索,性能显然比hash检索方式要慢一些了。

  4. 接口实现

    HashMap实现了Cloneable, Serializable SparseArray 实现了Cloneable 接口,也就是说SparseArray 是不支持序列化的。

3.代码解析-功法传授(Q增 W删 E改 R查)

先天优越之—>初始技能

 /**
* Creates a new SparseArray containing no mappings.
* 创建默认容器大小为10的SparseArray
*/
public SparseArray() {
this(10);
} public SparseArray(int initialCapacity) {...}

如虎添翼之 —>

/**
* Adds a mapping from the specified key to the specified value,
* replacing the previous mapping from the specified key if there
* was one.
通过指定的key和value加入一个键值对,假设这个位置已经存在一个了,则替换掉
*/
public void put(int key, E value) {
int i = ContainerHelpers.binarySearch(mKeys, mSize, key); if (i >= 0) {
mValues[i] = value;
} else {
i = ~i; if (i < mSize && mValues[i] == DELETED) {
mKeys[i] = key;
mValues[i] = value;
return;
} if (mGarbage && mSize >= mKeys.length) {
gc(); // Search again because indices may have changed.
i = ~ContainerHelpers.binarySearch(mKeys, mSize, key);
} mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key);
mValues = GrowingArrayUtils.insert(mValues, mSize, i, value);
mSize++;
}
} /**
* Puts a key/value pair into the array, optimizing for the case where
* the key is greater than all existing keys in the array.
通过指定的key和value加入一个键值对,在原有的基础上添加
*/
public void append(int key, E value) {
if (mSize != 0 && key <= mKeys[mSize - 1]) {
put(key, value);
return;
} if (mGarbage && mSize >= mKeys.length) {
gc();
} mKeys = GrowingArrayUtils.append(mKeys, mSize, key);
mValues = GrowingArrayUtils.append(mValues, mSize, value);
mSize++;
}

割袍断义之 —>

/**
* Removes the mapping from the specified key, if there was any.
* 从键值对中删除指定的key
*/
public void delete(int key) {
int i = ContainerHelpers.binarySearch(mKeys, mSize, key); if (i >= 0) {
if (mValues[i] != DELETED) {
mValues[i] = DELETED;
mGarbage = true;
}
}
} /**
* @hide
* 隐藏方法
* Removes the mapping from the specified key, if there was any, returning the old value.
* 从键值对中删除指定的key,假设在不论什么地方还实用到,会返回旧值。 */
public E removeReturnOld(int key) {
int i = ContainerHelpers.binarySearch(mKeys, mSize, key); if (i >= 0) {
if (mValues[i] != DELETED) {
final E old = (E) mValues[i];
mValues[i] = DELETED;
mGarbage = true;
return old;
}
}
return null;
} /**
* Alias for {@link #delete(int)}. (注意:这里是调用的delete 的方法,所以remove方法和 delete效果是一样的)
*/
public void remove(int key) {
delete(key);
}
/**
* Removes the mapping at the specified index.
* 删除指定index(注意这里不是删除指定的key,可是事实上我们能够发现这个实现是和delete方法是一样的)
*/
public void removeAt(int index) {
if (mValues[index] != DELETED) {
mValues[index] = DELETED;
mGarbage = true;
}
}
/**
* Remove a range of mappings as a batch.
* 删除一组数据
* @param index Index to begin at index 開始删除的位置
* @param size Number of mappings to remove 删除的长度
*/
public void removeAtRange(int index, int size) {
final int end = Math.min(mSize, index + size);
for (int i = index; i < end; i++) {
removeAt(i);
}
}

偷梁换柱之—>

/**
* Returns the index for which {@link #keyAt} would return the
* specified key, or a negative number if the specified
* key is not mapped.
* 返回key 相应的index,假设指定的key没有找到则返回一个负数
*/
public int indexOfKey(int key) {
if (mGarbage) {
gc();
} return ContainerHelpers.binarySearch(mKeys, mSize, key);
} /**
* Given an index in the range <code>0...size()-1</code>, sets a new
* value for the <code>index</code>th key-value mapping that this
* SparseArray stores.
改动指定index 下相应的value 值(注意这里 第一个參数是index,不是 key。能够配合indexOfKey(int key)先获得key所在的index)
*/
public void setValueAt(int index, E value) {
if (mGarbage) {
gc();
} mValues[index] = value;
}

按图索骥之—>

/**
* Gets the Object mapped from the specified key, or <code>null</code>
* if no such mapping has been made.
通过指定的key获取相应的Object,假设没有找到相应的键值对,会默认返回null
*/
public E get(int key) {
return get(key, null);
} /**
1. Gets the Object mapped from the specified key, or the specified Object
2. if no such mapping has been made.
3. 通过运行的key获取相应的Object,,假设没有找到相应的键值对。会返回设置的默认值
*/
@SuppressWarnings("unchecked")
public E get(int key, E valueIfKeyNotFound) {
int i = ContainerHelpers.binarySearch(mKeys, mSize, key); if (i < 0 || mValues[i] == DELETED) {
return valueIfKeyNotFound;
} else {
return (E) mValues[i];
}
}

寻名剑(键) —>获得key

/**
获得指定index 下的key
* */
public int keyAt(int index) {
if (mGarbage) {
gc();
}
return mKeys[index];
}

剑鞘—>获得value

/**
* 获得指定index 相应下的value
* */ public E valueAt(int index) {
if (mGarbage) {
gc();
} return (E) mValues[index];
}

一个萝卜一个坑 —>*依据value 获得index*

/**
* 依据指定的value 获得所在的index,假设没有 则返回-1
*/ public int indexOfValue(E value) {
if (mGarbage) {
gc();
} for (int i = 0; i < mSize; i++)
if (mValues[i] == value)
return i; return -1;
}

终极必杀之受死吧妖孽,打回原形 —>clear()

/**
* Removes all key-value mappings from this SparseArray.
* 从SparseArray 中移除所有的key-value 键值对
*/
public void clear() {
int n = mSize;
Object[] values = mValues; for (int i = 0; i < n; i++) {
values[i] = null;
} mSize = 0;
mGarbage = false;
}

SparseArray具体解释,我说SparseArray,你说要!的更多相关文章

  1. 关于Android中ArrayMap/SparseArray比HashMap性能好的深入研究

    由于网上有朋友对于这个问题已经有了很详细的研究,所以我就不班门弄斧了: 转载于:http://android-performance.com/android/2014/02/10/android-sp ...

  2. SparseArray<E>详解

    SparseArray<E> 是官方推荐的用来替代 HashMap<Integer, E> 的一个工具类,相比来说有着更好的性能(其核心是折半查找函数(binarySearch ...

  3. Android源码分析之SparseArray

    本来接下来应该分析MessageQueue了,可是我这几天正好在实际开发中又再次用到了SparseArray(之前有用到过一次,那次只是 大概浏览了下源码,没做深入研究),于是在兴趣的推动下,花了些时 ...

  4. Android内存优化(使用SparseArray和ArrayMap代替HashMap)

    在Android开发时,我们使用的大部分都是Java的api,比如HashMap这个api,使用率非常高,但是对于Android这种对内存非常敏感的移动平台,很多时候使用一些java的api并不能达到 ...

  5. 数据结构(二): 轻量级键值对 SparseArray

    SparseArray是Android framework中提供的轻量级的键值对数据结构,我们知道空间和效率从来都是相悖的,SparseArray的实现正是以时间来换取空间效率,适合小规模数据的存储. ...

  6. 数据结构HashMap(Android SparseArray 和ArrayMap)

    HashMap也是我们使用非常多的Collection,它是基于哈希表的 Map 接口的实现,以key-value的形式存在.在HashMap中,key-value总是会当做一个整体来处理,系统会根据 ...

  7. Android内存管理(15)SparseArray系列代替HashMap系列

    参考: https://liuzhichao.com/p/832.html http://www.2cto.com/kf/201311/255640.html 1,简介: SparseArray是an ...

  8. Android:使用SparseArray取代HashMap优化性能

    之前看到一篇关于adapter的文章用到了SparseArray,所以在这里写写关于SparseArray的使用方法. SparseArray是官方针对安卓所写的容器,与HashMap类似,只是性能比 ...

  9. Android内存优化(使用SparseArray和ArrayMap取代HashMap)

    在Android开发时,我们使用的大部分都是Java的api,比方HashMap这个api,使用率非常高,可是对于Android这样的对内存非常敏感的移动平台,非常多时候使用一些java的api并不能 ...

随机推荐

  1. iOS应用程序多语言本地化

    多语言在应用程序中一般有两种做法:一.程序中提供给用户自己选择的机会:二.根据当前用户当前移动设备的语言自动将我们的app切换对应语言. 第一种做法比较简单完全靠自己的发挥了,这里主要讲第二种做法,主 ...

  2. linux-排序-sort

    命令格式: sort [参数][源文件][-o 输出文件] 参数:  -b   忽略每行前面开始出的空格字符.  -c   检查文件是否已经按照顺序排序.  -f   排序时,忽略大小写字母.  -M ...

  3. RxJava 1.x 理解-2

    给RxJava 加入线程控制 -- Scheduler 在 RxJava 1.x 理解-1 中,我们说到了RxJava的简单用法,但是这还远远不够,因为这简单用法是在同一个线程中使用的.比如我们需要在 ...

  4. 虚拟内存,MMU/TLB,PAGE,Cache之间关系

    转:http://hi.baidu.com/gilbertjuly/item/6690ba0dfdf57adfdde5b040 虚拟地址VA到物理地址PA以页page为单位.通常page的大小为4K. ...

  5. 【docker】查看docker镜像的版本号TAG,从远程仓库拉取自己想要版本的镜像

    要想查看镜像的版本好TAG,需要在docker hub查看 地址如下:https://hub.docker.com/r/library/ 进入之后,在页面左上角搜索框搜索, 例如搜索redis 搜索完 ...

  6. XSS-Proxy

    关于XSS(cross site scripting),相信对此有过研究的人已经感受到了它的“魅力”,权威机构也公布了最近的安全数据,xss已经上升为第二大网络安全隐患: 于此我想通过此文浅析一下xs ...

  7. Linux 动态库 静态库

    什么是库 本质上来说库是一种可执行代码的二进制形式,可以被操作系统载入内存执行.由于windows和Linux的本质不同,因此二者库的二进制是不兼容的.Linux操作系统支持的库函数分为静态库和动态库 ...

  8. 【C++ OpenGL ES 2.0编程笔记】8: 使用VBO和IBO绘制立方体 【转】

    http://blog.csdn.net/kesalin/article/details/8351935 前言 本文介绍了OpenGL ES 2.0 中的顶点缓冲对象(VBO: Vertex Buff ...

  9. g++ 链接静态库命令应该放在最后

    昨天编译去年写的FloorServer,居然一堆错误: chu@chu-laptop:/media/E/work/github/FloorServer/FloorServer$ makeg++ -g ...

  10. HDU 4287-Intelligent IME(哈希)

    Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...