SparseArrayCompat为啥比HashMap节省内存开销?

避免了自动装箱键,并且它的数据结构不依赖于每个映射的额外条目对象

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 将整数映射到对象。与普通的对象数组不同,索引中可能存在间隙。与使用 HashMap 将整数映射到对象相比,它的内存效率更高,因为它避免了自动装箱键,并且它的数据结构不依赖于每个映射的额外条目对象。

SparseArrayCompat与SparseArray的区别:本质一回事,都是稀疏数组,前者兼容低版本(19以下),后者需要导入v4包才能兼容

猜你喜欢

转载自blog.csdn.net/xiyangyang8110/article/details/123229878
今日推荐