What convenience can the official core-ktx library bring to the development of the SparseArray series and Pair?

This article is a research official core-ktx库的第三篇文章, aiming to explore which tool classes or methods in the library can improve our development efficiency.

The reference links for the first two articles are as follows:

What conveniences can the official core-ktx library bring to development you need to know?

What convenience can the official core-ktx library bring to the development of rich text spans?

This article is mainly to study the convenience of core-ktxthe library for SparseArrayseries and Pairdevelopment code! !

SparseArrayseries

SparseArray, LongSparseArrayseries is a kind key-valueof encapsulation class that supports reading and writing in the form of data structure. Compared with HashMapit, there are two points:

image.png

  1. The key value keyis a basic data type, not a wrapper class corresponding to the basic data type, no need装箱
  2. Its data structure is an array of key values key​​and an array of valuevalues, unlike HashMap, based Entryon the key-valueencapsulation of pairs and then read and write

PS: There is another series of collections in Android SparseIntArray, SparseBooleanArray, SparseLongArray, these collections are more powerful, key和value都是基本数据类型, do not need boxing, do not confuse with the above collections. However, the extension packages provided by these two collections core-ktxare almost the same, so they will not be explained individually.

  1. SparseArray<T>.size()get collection size

    image.png

    This is nothing new, it just encapsulates the SparseArrayoriginal way of getting the size size(). With the help of kotlin's syntactic sugar, it can be 属性called as ( 本质上还是调用的方法):

    private fun test5() {
        val sparseArray: SparseArray<String> = SparseArray<String>()
        val size = sparseArray.size
    }
    复制代码
  2. SparseArray<T>.contains(key: Int)Judgment includes specifyingkey

    image.png

    You can see that this method operatoris modified, it is an operator overloading method, and the operator is overloaded, which is inused as follows:

    @RequiresApi(Build.VERSION_CODES.R)
    private fun test5() {
        val sparseArray: SparseArray<String> = SparseArray<String>()
        val isContain = 5 in sparseArray
    }
    复制代码

    不过这个api要求SDK>=30才能使用,我们可以直接将源码copy到项目工具类库中使用即可。

  3. SparseArray<T>.plus(other: SparseArray<T>)并合并两个集合为一个新集合

    image.png

    这也是一个运算符重载函数,重载了运算符+,项目中可以这样使用:

    private fun test5() {
        val sparseArray1: SparseArray<String> = SparseArray<String>()
        val sparseArray2: SparseArray<String> = SparseArray<String>()
        val newArray = sparseArray1 + sparseArray2
    }
    复制代码
  4. SparseArray<T>.containsValue(value: T)判断是否存在指定value

    image.png

    这个和上面的contains方法类似,不过这个是判断某个value值是否在该集合中,同样也是重载了运算符in

  5. SparseArray<T>.set(key: Int, value: T)写入数据

    image.png

    重载了运算符[]的函数,可以这样向集合中写入数据:

    private fun test5() {
        val sparseArray1: SparseArray<String> = SparseArray<String>()
        sparseArray1[10] = ""
    }
    复制代码
  6. SparseArray<T>.getOrDefault(key: Int, defaultValue: T)带默认值的读值

    image.png

    这个方法和HashMapgetOrDefault()类似,当获取的值为null时,就返回默认值:

    private fun test5() {
        val sparseArray1: SparseArray<String> = SparseArray<String>()
        sparseArray1.getOrDefault(10, "null")
    }
    复制代码
  7. SparseArray<T>.getOrElse(key: Int, defaultValue: () -> T)带默认函数类型返回值的读值

    image.png

    这个和上面的getOrDefault()比较像,只不过提供默认值的不再是一个固定的具体类型的值,而是一个灵活的函数类型,我们可以在这个函数类型中进行额外的逻辑编写:

    private fun test5() {
        val sparseArray1: SparseArray<String> = SparseArray<String>()
        sparseArray1.getOrElse(10) {
            val tmp = "哈哈哈${sparseArray1.size}"
            tmp
        }
    } 
    复制代码
  8. SparseArray<T>.isEmpty()判断集合是否为空的

    image.png

  9. SparseArray<T>.remove(key: Int, value: T)移除指定key-value

    image.png

  10. SparseArray<T>.forEach(action: (key: Int, value: T) -> Unit)遍历

    image.png

    使用:

    
    private fun test5() {
        val sparseArray1: SparseArray<String> = SparseArray<String>()
        sparseArray1.forEach { key, value ->
            //执行操作
        }
    }
    复制代码
  11. SparseArray<T>.keyIterator()遍历键值key

    image.png

    这个方法会返回一个迭代器,这样我们就可以使用for in来遍历键值key了:

    private fun test5() {
        val sparseArray1: SparseArray<String> = SparseArray<String>()
        for (key in sparseArray1.keyIterator()) {
            //执行操作
        }
    }
    复制代码

    自定义迭代器就可以帮助我们实现通过for in关键字遍历,具体的可以参考我之前写的一篇文章:你需要懂的Kotlin开发技巧之八#运算符重载for in

  12. SparseArray<T>.valueIterator()遍历value

    image.png

    使用起来和上面的keyIterator()方法类似。

Pair<F, S>系列

  1. componentX()解构

    image.png

    这也是一个operator修饰的运算符重载函数,关于这个解构,我不太该怎么去用语言来描述,大家直接看下使用吧:

    private fun test6() {
        val pair = Pair(10, "ha")
        val (key, value) = pair
    }
    复制代码

    就是可以直接将这个对象的内部属性赋值给局部声明的变量,如果你只使用key或value,可以将不想要解构的属性使用_代替: val (key , _) = pair //只使用key

    The extension method of this structure is specially provided for objects Java(including those under the android and androidX packages) , and it is not necessary to implement it yourself , because the declared object is one , and it will generate a destructuring method for us by default:PairKotlinPairPairdata classdata class

    image.png

    Decompile into java code to see the generated componentX()series of methods.

  2. javaconversion to and kotlinfromPair

  • toKotlinPairConvert androidx包the Pairobject to Kotlinthe Pairobject

    image.png

  • toAndroidXPairConvert kotlinthe Pairobject to androidx包the Pairobject

    image.png

  • toKotlinPairConvert android包the Pairobject to Kotlinthe Pairobject

    image.png

  • toAndroidPairConvert kotlinthe Pairobject to android包the Pairobject

    image.png

Summarize

For the detailed source code, please refer to the source code of files such as , , , etc. core-ktxunder the package . Next, I am ready to study some extension tools for operations under the package (many, many categories):SparseArray.ktPair.ktSparseXXXArray.ktcore-ktxgraphicsView

image.png
Take a look at what convenience it can bring to our common Viewdevelopment, please stay tuned! !

I am participating in the recruitment of the creator signing program of the Nuggets Technology Community, click the link to register and submit .

Guess you like

Origin juejin.im/post/7118765812105609230