android 替代map,Android应用性能优化之使用SparseArray替代HashMap

HashMap是java里比较经常使用的一个集合类,我比较习惯用来缓存一些处理后的结果。最近在作一个Android项目,在代码中定义这样一个变量,实例化时,Eclipse却给出了一个 performance 警告。 java

1c78ee7fbfc3408b82bf0825.html

意 思就是说用SparseArray来替代,以获取更好性能。老实说,对SparseArray并不熟悉,第一感受应该是Android 提供的一个类。按住Ctrl点击进入SparseArray的源码,果不其然,肯定是Android提供的一个工具类。 android

1c78ee7fbfc3408b82bf0825.html

单纯从字面上来理解,SparseArray指的是稀疏数组(Sparse array),所谓稀疏数组就是数组中大部分的内容值都未被使用(或都为零),在数组中仅有少部分的空间使用。所以形成内存空间的浪费,为了节省内存空间,而且不影响数组中原有的内容值,咱们能够采用一种压缩的方式来表示稀疏数组的内容。 算法

假设有一个9*7的数组,其内容以下: 数组

1c78ee7fbfc3408b82bf0825.html

在此数组中,共有63个空间,但却只使用了5个元素,形成58个元素空间的浪费。如下咱们就使用稀疏数组从新来定义这个数组: 缓存

1c78ee7fbfc3408b82bf0825.html

其中在稀疏数组中第一部分所记录的是原数组的列数和行数以及元素使用的个数、第二部分所记录的是原数组中元素的位置和内容。通过压缩以后,原来须要声明大小为63的数组,而使用压缩后,只须要声明大小为6*3的数组,仅需18个存储空间。 app

继续阅读SparseArray的源码,从构造方法咱们能够看出,它和通常的List同样,能够预先设置容器大小,默认的大小是10: ide

1

2

3

4

5

6

7

8

9

10

11

public

SparseArray

(

)

{

this

(

10

)

;

}

public

SparseArray

(

int

initialCapacity

)

{

initialCapacity

=

ArrayUtils

.

idealIntArraySize

(

initialCapacity

)

;

mKeys

=

new

int

[

initialCapacity

]

;

mValues

=

new

Object

[

initialCapacity

]

;

mSize

=

0

;

}

再来看看它对数据的“增删改查”。 函数

它有两个方法能够添加键值对: 工具

1

2

public

void

put

(

int

key

,

E

value

)

{

}

public

void

append

(

int

key

,

E

value

)

{

}

有四个方法能够执行删除操做: 性能

1

2

3

4

public

void

delete

(

int

key

)

{

}

public

void

remove

(

int

key

)

{

}

//直接调用的delete(int key)

public

void

removeAt

(

int

index

)

{

}

public

void

clear

(

)

{

}

修 改数据起初觉得只有setValueAt(int index, E value)能够修改数据,但后来发现put(int key, E value)也能够修改数据,咱们查看put(int key, E value)的源码可知,在put数据以前,会先查找要put的数据是否已经存在,若是存在就是修改,不存在就添加。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

public

void

put

(

int

key

,

E

value

)

{

int

i

=

binarySearch

(

mKeys

,

0

,

mSize

,

key

)

;

if

(

i

&

gt

;

=

0

)

{

mValues

[

i

]

=

value

;

}

else

{

i

=

~

i

;

if

(

i

&

lt

;

mSize

&

amp

;

&

amp

;

mValues

[

i

]

==

DELETED

)

{

mKeys

[

i

]

=

key

;

mValues

[

i

]

=

value

;

return

;

}

if

(

mGarbage

&

amp

;

&

amp

;

mSize

&

gt

;

=

mKeys

.

length

)

{

gc

(

)

;

// Search again because indices may have changed.

i

=

~

binarySearch

(

mKeys

,

0

,

mSize

,

key

)

;

}

…………

因此,修改数据实际也有两种方法:

1

2

public

void

put

(

int

key

,

E

value

)

public

void

setValueAt

(

int

index

,

E

value

)

最后再来看看如何查找数据。有两个方法能够查询取值:

1

2

public

E

get

(

int

key

)

public

E

get

(

int

key

,

E

valueIfKeyNotFound

)

其中get(int key)也只是调用了 get(int key,E valueIfKeyNotFound),最后一个从传参的变量名就能看出,传入的是找不到的时候返回的值.get(int key)当找不到的时候,默认返回null。

查看第几个位置的键:

1

public

int

keyAt

(

int

index

)

有一点须要注意的是,查看键所在位置,因为是采用二分法查找键的位置,因此找不到时返回小于0的数值,而不是返回-1。返回的负值是表示它在找不到时所在的位置。

查看第几个位置的值:

1

public

E

valueAt

(

int

index

)

查看值所在位置,没有的话返回-1:

1

public

int

indexOfValue

(

E

value

)

最后,发现其核心就是折半查找函数(binarySearch),算法设计的很不错。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

private

static

int

binarySearch

(

int

[

]

a

,

int

start

,

int

len

,

int

key

)

{

int

high

=

start

+

len

,

low

=

start

-

1

,

guess

;

while

(

high

-

low

&

gt

;

1

)

{

guess

=

(

high

+

low

)

/

2

;

if

(

a

[

guess

]

&

lt

;

key

)

low

=

guess

;

else

high

=

guess

;

}

if

(

high

==

start

+

len

)

return

~

(

start

+

len

)

;

else

if

(

a

[

high

]

==

key

)

return

high

;

else

return

~

high

;

}

相应的也有SparseBooleanArray,用来取代HashMap,SparseIntArray用来取代HashMap,你们有兴趣的能够研究。

总结:SparseArray是android里为这样的Hashmap而专门写的类,目的是提升效率,其核心是折半查找函数(binarySearch)。在Android中,当咱们须要定义

1

HashMap

<

Integer

,

E

>

hashMap

=

new

HashMap

<

Integer

,

E

>

(

)

;

时,咱们可使用以下的方式来取得更好的性能.

1

SparseArray

sparseArray

=

new

SparseArray

(

)

;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值