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-ktx
the library forSparseArray
series andPair
development code! !
SparseArray
series
SparseArray
,LongSparseArray
series is a kindkey-value
of encapsulation class that supports reading and writing in the form of data structure. Compared withHashMap
it, there are two points:
- The key value
key
is a basic data type, not a wrapper class corresponding to the basic data type, no need装箱
- Its data structure is an array of key values
key
and an array ofvalue
values, unlikeHashMap
, basedEntry
on thekey-value
encapsulation 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 collectionscore-ktx
are almost the same, so they will not be explained individually.
-
SparseArray<T>.size()
get collection sizeThis is nothing new, it just encapsulates the
SparseArray
original way of getting the sizesize()
. 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 } 复制代码
-
SparseArray<T>.contains(key: Int)
Judgment includes specifyingkey
You can see that this method
operator
is modified, it is an operator overloading method, and the operator is overloaded, which isin
used 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到项目工具类库中使用即可。 -
SparseArray<T>.plus(other: SparseArray<T>)
并合并两个集合为一个新集合这也是一个运算符重载函数,重载了运算符
+
,项目中可以这样使用:private fun test5() { val sparseArray1: SparseArray<String> = SparseArray<String>() val sparseArray2: SparseArray<String> = SparseArray<String>() val newArray = sparseArray1 + sparseArray2 } 复制代码
-
SparseArray<T>.containsValue(value: T)
判断是否存在指定value
这个和上面的
contains
方法类似,不过这个是判断某个value值是否在该集合中,同样也是重载了运算符in
-
SparseArray<T>.set(key: Int, value: T)
写入数据重载了运算符
[]
的函数,可以这样向集合中写入数据:private fun test5() { val sparseArray1: SparseArray<String> = SparseArray<String>() sparseArray1[10] = "" } 复制代码
-
SparseArray<T>.getOrDefault(key: Int, defaultValue: T)
带默认值的读值这个方法和
HashMap
的getOrDefault()
类似,当获取的值为null时,就返回默认值:private fun test5() { val sparseArray1: SparseArray<String> = SparseArray<String>() sparseArray1.getOrDefault(10, "null") } 复制代码
-
SparseArray<T>.getOrElse(key: Int, defaultValue: () -> T)
带默认函数类型返回值的读值这个和上面的
getOrDefault()
比较像,只不过提供默认值的不再是一个固定的具体类型的值,而是一个灵活的函数类型,我们可以在这个函数类型中进行额外的逻辑编写:private fun test5() { val sparseArray1: SparseArray<String> = SparseArray<String>() sparseArray1.getOrElse(10) { val tmp = "哈哈哈${sparseArray1.size}" tmp } } 复制代码
-
SparseArray<T>.isEmpty()
判断集合是否为空的 -
SparseArray<T>.remove(key: Int, value: T)
移除指定key-value
-
SparseArray<T>.forEach(action: (key: Int, value: T) -> Unit)
遍历使用:
private fun test5() { val sparseArray1: SparseArray<String> = SparseArray<String>() sparseArray1.forEach { key, value -> //执行操作 } } 复制代码
-
SparseArray<T>.keyIterator()
遍历键值key
这个方法会返回一个迭代器,这样我们就可以使用
for in
来遍历键值key
了:private fun test5() { val sparseArray1: SparseArray<String> = SparseArray<String>() for (key in sparseArray1.keyIterator()) { //执行操作 } } 复制代码
自定义迭代器就可以帮助我们实现通过
for in
关键字遍历,具体的可以参考我之前写的一篇文章:你需要懂的Kotlin开发技巧之八#运算符重载for in
。 -
SparseArray<T>.valueIterator()
遍历value使用起来和上面的
keyIterator()
方法类似。
Pair<F, S>
系列
-
componentX()
解构这也是一个
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:Pair
Kotlin
Pair
Pair
data class
data class
Decompile into java code to see the generated
componentX()
series of methods. -
java
conversion to andkotlin
fromPair
-
toKotlinPair
Convertandroidx包
thePair
object toKotlin
thePair
object -
toAndroidXPair
Convertkotlin
thePair
object toandroidx包
thePair
object -
toKotlinPair
Convertandroid包
thePair
object toKotlin
thePair
object -
toAndroidPair
Convertkotlin
thePair
object toandroid包
thePair
object
Summarize
For the detailed source code, please refer to the source code of files such as , , , etc. core-ktx
under the package . Next, I am ready to study some extension tools for operations under the package (many, many categories):SparseArray.kt
Pair.kt
SparseXXXArray.kt
core-ktx
graphics
View
Take a look at what convenience it can bring to our common View
development, 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 .