## SparseArray

## SparseBooleanArray

## SparseIntArray

## SparseLongArray

* 位于android.util,Android 中的数据结构,针对移动端做了优化,在数据量比较少的情况下,性能会好过 HashMap,类似于 HashMap,key:int ,value:object 。

* 为什么性能会好:

1. key  和 value  采用数组进行存储。存储 key 的数组是 int 类型,不需要进行装箱操作。提供了速度。

2. 采用二分查找法,在插入进行了排序,所以两个数组是按照从小到大进行排序的。

3. 在查找的时候,进行二分查找,数据量少的情况下,速度比较快。

同时还有一个类:SparseArrayCompat,位于android.support.v4.util,与 SparseArray 一样的。

其中有好几个类似的数据结构:SparseBooleanArray(key:int, value:boolean), SparseIntArray(key:int, value:int), SparseLongArray(key:int, value:long). SparseArray 通过数组对 key , value 进行存储。

在 map Integers to Objects 方面进行了优化,所以比使用 HashMap 提高了性能,建议遇到key:int, value:object 的情况下使用。

构造方法:

默认构造方法中会创建长度为 10 的数组来分别储存 key 和 value。

可以自己在手动输入一个数字,来创建一个指定长度的数组来存储key 和 value。

增:

public void put(int key, E value)  // 存储指定key 和指定value ,假如指定key 已经有了在映射中,替换掉已经存在的。
public void append(int key, E value) // 储存 key value 到数组,key 的索引应该大于数组中的已有索引

删:

public void delete(int key)  // 删除指定key
public void remove(int key) // 调用 delete 方法
public void removeAt(int index) // 删除指定索引
public void removeAtRange(int index, int size) //删除指定范围的索引
public void clear() //清空所有内容

改:

public void put(int key, E value)  // 指定key 已存在的情况下,更新,不存在的情况下,增加
public void setValueAt(int index, E value) // 更新指定索引的 value

查:

public E get(int key)   // 得到指定key 的 value,调用的是 get(int key, null)
public E get(int key, E valueIfKeyNotFound) // 返回指定key 的value,没有的情况下,返回传入的默认值
public int indexOfKey(int key) // 传入key 的索引
public int indexOfValue(E value) // 传入 value 的索引
public int keyAt(int index) // 指定索引的 key
public E valueAt(int index) //指定索引的 value

源码:

 public class SparseArray<E> implements Cloneable {
private static final Object DELETED = new Object();
private boolean mGarbage = false; private int[] mKeys;
private Object[] mValues;
private int mSize; /**
* Creates a new SparseArray containing no mappings.
*/
public SparseArray() {
this(10);
} /**
* Creates a new SparseArray containing no mappings that will not
* require any additional memory allocation to store the specified
* number of mappings. If you supply an initial capacity of 0, the
* sparse array will be initialized with a light-weight representation
* not requiring any additional array allocations.
*/
public SparseArray(int initialCapacity) {
if (initialCapacity == 0) {
mKeys = EmptyArray.INT;
mValues = EmptyArray.OBJECT;
} else {
mValues = ArrayUtils.newUnpaddedObjectArray(initialCapacity);
mKeys = new int[mValues.length];
}
mSize = 0;
} @Override
@SuppressWarnings("unchecked")
public SparseArray<E> clone() {
SparseArray<E> clone = null;
try {
clone = (SparseArray<E>) super.clone();
clone.mKeys = mKeys.clone();
clone.mValues = mValues.clone();
} catch (CloneNotSupportedException cnse) {
/* ignore */
}
return clone;
} /**
* Gets the Object mapped from the specified key, or <code>null</code>
* if no such mapping has been made.
*/
public E get(int key) {
return get(key, null);
} /**
* Gets the Object mapped from the specified key, or the specified Object
* if no such mapping has been made.
*/
@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];
}
} /**
* Removes the mapping from the specified key, if there was any.
*/
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.
*/
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)}.
*/
public void remove(int key) {
delete(key);
} /**
* Removes the mapping at the specified index.
*/
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
* @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);
}
} private void gc() {
// Log.e("SparseArray", "gc start with " + mSize); int n = mSize;
int o = 0;
int[] keys = mKeys;
Object[] values = mValues; for (int i = 0; i < n; i++) {
Object val = values[i]; if (val != DELETED) {
if (i != o) {
keys[o] = keys[i];
values[o] = val;
values[i] = null;
} o++;
}
} mGarbage = false;
mSize = o; // Log.e("SparseArray", "gc end with " + mSize);
} /**
* Adds a mapping from the specified key to the specified value,
* replacing the previous mapping from the specified key if there
* was one.
*/
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++;
}
} /**
* Returns the number of key-value mappings that this SparseArray
* currently stores.
*/
public int size() {
if (mGarbage) {
gc();
} return mSize;
} /**
* Given an index in the range <code>0...size()-1</code>, returns
* the key from the <code>index</code>th key-value mapping that this
* SparseArray stores.
*
* <p>The keys corresponding to indices in ascending order are guaranteed to
* be in ascending order, e.g., <code>keyAt(0)</code> will return the
* smallest key and <code>keyAt(size()-1)</code> will return the largest
* key.</p>
*/
public int keyAt(int index) {
if (mGarbage) {
gc();
} return mKeys[index];
} /**
* Given an index in the range <code>0...size()-1</code>, returns
* the value from the <code>index</code>th key-value mapping that this
* SparseArray stores.
*
* <p>The values corresponding to indices in ascending order are guaranteed
* to be associated with keys in ascending order, e.g.,
* <code>valueAt(0)</code> will return the value associated with the
* smallest key and <code>valueAt(size()-1)</code> will return the value
* associated with the largest key.</p>
*/
@SuppressWarnings("unchecked")
public E valueAt(int index) {
if (mGarbage) {
gc();
} return (E) mValues[index];
} /**
* 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.
*/
public void setValueAt(int index, E value) {
if (mGarbage) {
gc();
} mValues[index] = value;
} /**
* Returns the index for which {@link #keyAt} would return the
* specified key, or a negative number if the specified
* key is not mapped.
*/
public int indexOfKey(int key) {
if (mGarbage) {
gc();
} return ContainerHelpers.binarySearch(mKeys, mSize, key);
} /**
* Returns an index for which {@link #valueAt} would return the
* specified key, or a negative number if no keys map to the
* specified value.
* <p>Beware that this is a linear search, unlike lookups by key,
* and that multiple keys can map to the same value and this will
* find only one of them.
* <p>Note also that unlike most collections' {@code indexOf} methods,
* this method compares values using {@code ==} rather than {@code equals}.
*/
public int indexOfValue(E value) {
if (mGarbage) {
gc();
} for (int i = 0; i < mSize; i++)
if (mValues[i] == value)
return i; return -1;
} /**
* Removes all key-value mappings from this SparseArray.
*/
public void clear() {
int n = mSize;
Object[] values = mValues; for (int i = 0; i < n; i++) {
values[i] = null;
} mSize = 0;
mGarbage = false;
} /**
* Puts a key/value pair into the array, optimizing for the case where
* the key is greater than all existing keys in the array.
*/
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++;
} /**
* {@inheritDoc}
*
* <p>This implementation composes a string by iterating over its mappings. If
* this map contains itself as a value, the string "(this Map)"
* will appear in its place.
*/
@Override
public String toString() {
if (size() <= 0) {
return "{}";
} StringBuilder buffer = new StringBuilder(mSize * 28);
buffer.append('{');
for (int i=0; i<mSize; i++) {
if (i > 0) {
buffer.append(", ");
}
int key = keyAt(i);
buffer.append(key);
buffer.append('=');
Object value = valueAt(i);
if (value != this) {
buffer.append(value);
} else {
buffer.append("(this Map)");
}
}
buffer.append('}');
return buffer.toString();
}
}

##  ArrayMap

* 与 SparseArray 类似

Android 移动端数据结构的更多相关文章

  1. Android 服务端开发之开发环境配置

    Android 服务端开发之开发环境配置 这里是在Eclipse的基础上安装PhpEclipse插件方法,PHPEclipse是Eclipse的 一个用于开发PHP的插件.当然也可以采用Java开发a ...

  2. erlang-百度云推送Android服务端功能实现-erlang

    百度云推送官方地址http://developer.baidu.com/wiki/index.php?title=docs/cplat/push 简单的介绍下原理: 百度云推送支持IOS和Androi ...

  3. Android中的数据结构

    数据结构在Android中也有着大量的运用,这里采用数据结构与源代码分析相结合,来认识Android的数据结构 线性表 线性表可分为顺序存储结构和链式存储结构 顺序存储结构-ArrayList 通过对 ...

  4. delphi xe5 android 服务端和手机端的源码下载

    xe5 android的服务端和手机客户端的源代码下载地址 http://files.cnblogs.com/nywh2008/AndroidTest.rar

  5. Android服务端本地窗口FramebufferNativeWindow

    Android窗口系统 我们知道Android系统采用OpenGL来绘制3D图形,OpenGL ES提供了本地窗口(NativeWindow)的概念,无论是在Android平台中还是其他平台中,只要实 ...

  6. 实现Android Native端爆破源码

    尝试在移动端so侧做一些内存修改,使之走向不通的逻辑,一下为将要爆破的APP源码 JAVA侧: package com.example.grady.sectestone; import android ...

  7. 浏览器与android移动端视频互播技术实现

    手机端与平台之间的视频直播功能,主要通过集成多种开源视频框架以及采购第三方视频直播服务器产品来实现预定业务需求.视频直播对话功能的实现,主要经历了三个阶段:利用开源视频框架实现视频直播.采购第三方视频 ...

  8. Android移动端网络优化

    介绍下针对移动端的网络优化,不限于 Android,同样适用于 iOS 和 H5 本文为性能优化系列第四篇,目前性能调优专题已完成以下部分: 性能优化总纲——性能问题及性能调优方式 性能优化第四篇—— ...

  9. Android客户端与PC服务端、android服务端通过WiFi通信

    前期准备:我的是Linux Mint操作系统(总之折腾的过程中怀疑过是不是系统的问题),首先是要创建wifi热点给android手机使用,这个时候笔记本作为通信的服务器端,android手机作为客户端 ...

随机推荐

  1. JQuery EasyUI学习笔记

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6690888.html  简介与准备 jQuery EasyUI 是一个基于 jQuery 的框架,集成了各种用 ...

  2. BI Admin Tools和目录管理器的连接配置

    BI管理(Admin Tools): BI Server 的管理工具,用来创建维护模型,并且能够管理安全,会话,变量等等.是用的最多的一个管理工具:这个工具可以编辑 rpd 文件也可以在线通过 ODB ...

  3. PhoneGap开发的android项目环境搭建简单流程

      首先已经下载好最新的PhoneGap源代码. 已经装eclipse,android sdk及adt的安装和配置. 1.进入eclipse界面,新建android工程 new > androi ...

  4. java与C#、.NET AES加密、解密 解决方案

      1.情景展示 Java提供的密钥,C#无法解密. 2.原因分析 在Java中,AES的实际密钥需要用到KeyGenerator 和 SecureRandom,但是C#和.NET 里面没有这2个类, ...

  5. 【PMP】十五至尊图

    以上是PMP的10大知识领域与5个过程组,在PMP考试中属于必须记忆的知识,该知识来源于PMBOK 第6版 附件为每日练习记忆模板,可以更好的记忆上图 点击下载附件

  6. Linux 安全密钥验证

    [root@rhel7 ~]# ssh-keygen --在客户端主机中生成“密钥对” Generating public/private rsa key pair. Enter file in wh ...

  7. 如何在 CentOS 7 中安装或升级最新的内核

    虽然有些人使用 Linux 来表示整个操作系统,但要注意的是,严格地来说,Linux 只是个内核.另一方面,发行版是一个完整功能的系统,它建立在内核之上,具有各种各样的应用程序工具和库. 在正常操作期 ...

  8. Docker运行python容器

    容器是镜像运行的实例,而镜像保存在仓库里,测试或者发布生产环境只需要pull下来即可,相对传统的应用部署,能很好的保持环境的一致,节省运维时间.最近公司内部的java和.net服务也已经全部容器化,实 ...

  9. 【LeetCode】215. Kth Largest Element in an Array (2 solutions)

    Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...

  10. Log4j 基本配置示例

    1.log4j.properties log4j.rootLogger=INFO, stdout, fileout log4j.logger.TestProject=INFO log4j.append ...