日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

HashMap can be replaced with SparseArray--Android应用性能优化之使用SparseArray替代HashMap

發布時間:2024/4/15 Android 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HashMap can be replaced with SparseArray--Android应用性能优化之使用SparseArray替代HashMap 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

HashMap是java里比較常用的一個集合類,我比較習慣用來緩存一些處理后的結果。最近在做一個Android項目,在代碼中定義這樣一個變量,實例化時,Eclipse卻給出了一個 performance 警告。



意思就是說用SparseArray <E>?來替代,以獲取更好性能。老實說,對SparseArray并不熟悉,第一感覺應該是Android提供的一個類。按住Ctrl點擊進入SparseArray的源碼,果不其然,確定是Android提供的一個工具類。


單純從字面上來理解,SparseArray指的是稀疏數組(Sparse array)?,所謂稀疏數組就是數組中大部分的內容值都未被使用(或都為零),在數組中僅有少部分的空間使用。因此造成內存空間的浪費,為了節省內存空間,并且不影響數組中原有的內容值,我們可以采用一種壓縮的方式來表示稀疏數組的內容

假設有一個9*7的數組,其內容如下:



其中在稀疏數組中第一部分所記錄的是原數組的列數和行數以及元素使用的個數、第二部分所記錄的是原數組中元素的位置和內容。經過壓縮之后,原來需要聲明大小為63的數組,而使用壓縮后,只需要聲明大小為6*3的數組,僅需18個存儲空間。

繼續閱讀SparseArray的源碼,從構造方法我們可以看出,它和一般的List一樣,可以預先設置容器大小,默認的大小是10:

public SparseArray() {this(10);}public SparseArray(int initialCapacity) {initialCapacity = ArrayUtils.idealIntArraySize(initialCapacity);mKeys = new int[initialCapacity];mValues = new Object[initialCapacity];mSize = 0;}

再來看看它對數據的“增刪改查”。

(1)它有兩個方法可以添加鍵值對:

public void put(int key, E value) {}public void append(int key, E value){}
(2)有四個方法可以執行刪除操作:
public void delete(int key) {}public void remove(int key) {} //直接調用的delete(int key)public void removeAt(int index){}public void clear(){}
(3)修改操作 修改數據起初以為只有setValueAt(int index, E value)可以修改數據,但后來發現put(int key, E value)也可以修改數據,我們查看put(int key, E value)的源碼可知,在put數據之前,會先查找要put的數據是否已經存在,如果存在就是修改,不存在就添加。
public void put(int key, E value) {int i = binarySearch(mKeys, 0, 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 = ~binarySearch(mKeys, 0, mSize, key);}…………
所以,修改數據實際也有兩種方法: public void put(int key, E value)public void setValueAt(int index, E value)
(4)最后再來看看如何查找數據。有兩個方法可以查詢取值:
public E get(int key)public E get(int key, E valueIfKeyNotFound)

其中get(int key)也只是調用了 get(int key,E valueIfKeyNotFound),最后一個從傳參的變量名就能看出,傳入的是找不到的時候返回的值.get(int key)當找不到的時候,默認返回null。

查看第幾個位置的鍵:public int keyAt(int index)
有一點需要注意的是,查看鍵所在位置,由于是采用二分法查找鍵的位置,所以找不到時返回小于0的數值,而不是返回-1。返回的負值是表示它在找不到時所在的位置。

查看第幾個位置的值:
public E valueAt(int index)
查看值所在位置,沒有的話返回-1:
public int indexOfValue(E value)
最后,發現其核心就是折半查找函數(binarySearch),算法設計的很不錯。

private static int binarySearch(int[] a, int start, int len, int key) {int high = start + len, low = start - 1, guess;while (high - low > 1) {guess = (high + low) / 2;if (a[guess] < key)low = guess;elsehigh = guess;}if (high == start + len)return ~(start + len);else if (a[high] == key)return high;elsereturn ~high;}

相應的也有SparseBooleanArray,用來取代HashMap <Integer, Boolean>?,SparseIntArray用來取代HashMap <Integer, Integer>?,大家有興趣的可以研究。

總結:
SparseArray是android里為<Interger,Object>?這樣的Hashmap而專門寫的類,目的是提高效率,其核心是折半查找函數(binarySearch)。在Android中,當我們需要定義
HashMap <Integer, E> hashMap = new HashMap <Integer, E> ();
時,我們可以使用如下的方式來取得更好的性能.
SparseArray <E> sparseArray = new SparseArray <E> ();

原文URL:https://liuzhichao.com/p/832.html

=====================

SparseArray

extends?Object
implements?Cloneable
java.lang.Object
???? android.util.SparseArray<E>

Class Overview


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.

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%.

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.

It is possible to iterate over the items in this container using?keyAt(int)?and?valueAt(int). Iterating over the keys using?keyAt(int)?with 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).


摘自:http://developer.android.com/reference/android/util/SparseArray.html

總結

以上是生活随笔為你收集整理的HashMap can be replaced with SparseArray--Android应用性能优化之使用SparseArray替代HashMap的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。