Android Development Seventeen: a collection of SparseArray, ArrayMap Detailed

Foreword


As a Anndroid developers, Java language we use when in most cases, natural in dealing with some of the data collection framework is to use Java, such as HashMap, HashSet, etc., but can you know, Android because of their special needs, but also customize their needs in the "exclusive" collection classes, refer to the official document, under android.util bag, capturing a total of several categories as follows: SparseArray series (SparseArray, SparseBooleanArray, SparseIntArray, SparseLongArray, LongSparseArray), and ArrayMap, ArraySet, I believe that even if never learned, see these class names, basically would have guessed some of their differences and usage, and here we come to a good school to learn them, begin!

table of Contents


1. Use 
2. feel the beauty of design 
3. advantages and disadvantages and application scenarios

text


Use
according to my habit, I think no matter what school, the first need is to use "it" feel it's usage, followed in order to talk about things in theory, let's learn how to use. 
First we look at the use of SparseArray

       

 //声明
        SparseArray<String> sparseArray= new SparseArray<>();
        //增加元素,append方式
        sparseArray.append(0, "myValue");
        //增加元素,put方式
        sparseArray.put(1, "myValue");
        //删除元素,二者等同
        sparseArray.remove(1);
        sparseArray.delete(1);
        //修改元素,put或者append相同的key值即可
        sparseArray.put(1,"newValue");
        sparseArray.append(1,"newValue");
        //查找,遍历方式1
        for(int i=0;i<sparseArray.size();i++){
            Log.d(TAG,sparseArray.valueAt(i));
        }
        //查找,遍历方式2
        for(int i=0;i<sparseArray.size();i++){
            int key = sparseArray.keyAt(i);
            Log.d(TAG,sparseArray.get(key));
        }


The OK, the normal usage, and other data structures hashmap substantially the same. 
The only difference is the type of key and value, hashmap the key value and the value argument is generic, but only SparseArray key values of type int, the value argument of type Object, to see this, you may find it very strange that not by a lot of constraints on the use Well, the significance of such constraints there?

Before you do, take a look at the rest of the SparseArray twin brothers and sisters, LongSparseArray and SparseArray compared, the only difference is the key value is long, so, since as long, then the relative SparseArray, it can store data elements the more than SparseArray.

Incidentally refresher, int the range -2 ^ 31 to 2 ^ 31-1, and long -2 ^ 63 to 2 ^ 63-1

Then turn the SparseBooleanArray, SparseIntArray, SparseLongArray, three brothers value is relatively speaking SparseArray values ​​are determined, SparseBooleanArray fixed value of the boolean type, SparseIntArray fixed value of type int, SparseLongArray value of fixed type long.

Note that the value of the value type boolean, int, long are lowercase, means a basic type, package type instead of

A brief summary of what follows

SparseArray          <int, Object>
LongSparseArray      <long, Object>
SparseBooleanArray   <int, boolean>
SparseIntArray       <int, int>
SparseLongArray      <int, long>



ok, then we will look at the use of ArrayMap and ArraySet

       

ArrayMap<String,String> map=new ArrayMap<>();
        //增加
        map.put("xixi","haha");
        //删除
        map.remove("xixi");
        //修改,put相同的key值即可
        map.put("xixi2","haha");
        map.put("xixi2","haha2");
        //查找,通过key来遍历
        for(String key:map.keySet()){
            Log.d(TAG,map.get(key));
        }


OK, normal usage, and HashMap is no different. ArraySet I would not continue to say it, their relationship is like as HashMap and HashSet, and it is the same HashSet can not store elements.

Additional explain, ArraySet use requires a minimum sdk version 23, which is minSdkVersion value must be greater than or equal 23

Published 377 original articles · won praise 145 · views 210 000 +

Guess you like

Origin blog.csdn.net/Windgs_YF/article/details/104298890