SparseArray简单使用

SparseArray是Android中一种高效的对象存储结构,相对于HashMap,它节省内存且避免了自动装箱。虽然查找速度较慢,适用于小规模数据,但删除优化使其在内存效率上更具优势。本文介绍了SparseArray的基本用法,并通过一个简单的示例展示了其在实际应用中的使用。
摘要由CSDN通过智能技术生成

先看看官网的介绍:

android.util.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.

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.

翻译后:

SparseArrays将整数映射到对象。与普通的对象数组不同,索引中可能存在间隙。与使用HashMap将Integers映射到Objects相比&#x

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值