Android - Kotiln basics tutorial

Posted by Julian Pedley on Thu, 11 Nov 2021 04:30:35 +0100

preface

In the previous article, we mainly explained the knowledge points corresponding to Kotlin string, number type and standard library function. In this article, we will explain the knowledge points related to Kotlin set.

1. List set

1.1 read only set

val list = listOf("json", "jack", "jacky")
fun main{
    //Read only list; Cannot write, cannot change properties
    println(list.getOrElse(10) { "Unknown" })
    println(list.getOrNull(10))
    println(list.getOrNull(10) ?: "Unknown")
    //list+="sdfdsf" direct error reporting!
}

When using listOf to create a set, the corresponding set can only be read and cannot be modified. Forced modification directly reports a syntax error!
Other reading methods:

  • The getOrElse method indicates that if it is not obtained, the contents of the closure after else will be returned!
  • The getOrNull method indicates that if it is not obtained, it will directly return null!

Operation effect

Unknown
null
Unknown

Here we see. The collection created by listOf can only be read and cannot be modified. What if you want a modifiable collection?

1.2 modifiable set

fun main{
    val mutableList = mutableListOf("json", "jack", "jacky")
    mutableList += "tom"
    println(mutableList)
    mutableList -= "jack"
    println(mutableList)
    mutableList.removeIf { it.contains("tom") }
    println(mutableList)
}

Operation results

[json, jack, jacky, tom]
[json, jacky, tom]
[json, jacky]

The collection defined by mutableListOf can be added and deleted. You can also call some logical methods to modify the collection.

1.3 traversal set

val list = listOf("json", "jack", "jacky")
fun main() {
    //Traversal set
    for (s in list) {
        println(s)
    }
    list.forEach {
        println(it)
    }
    list.forEachIndexed { index, s ->

        println("$index   $s")
    }
}

Operation results

json
jack
jacky
json
jack
jacky
0   json
1   jack
2   jacky

It's all very simple, direct and fast.

1.4 set deconstruction

val list = listOf("json", "jack", "jacky")
fun main{
    // deconstruction
    val (origin, _, proxy) = list
    //println("$") reports an error
    println("$origin   $proxy    ")
}

Operation effect

json    jacky

Note that val () syntax is used here to split the data in the set list. Second parameter_ An underscore indicates that the current deconstruction is skipped and cannot be used. Forced use will report a syntax error.

2. set set

2.1 read only set

fun main{
    // Read only collection
    val set = setOf("kotlin", "java", "scala")
    set.elementAt(2).run(::println)
}

Operation effect

scala

Like the above, it can only be read-only and cannot be modified.

2.2 variable set

fun main{
    val mutableSet = mutableSetOf("kotlin", "java", "scala")
    mutableSet += "python"
    println(mutableSet)
    mutableSet.elementAtOrElse(10) { "error" }.run(::println)
}

Operation effect

[kotlin, java, scala, python]
error

When mutableSetOf is used to define a set set, you can modify the set.

3. Assembly and de duplication

fun main{
  listOf("json", "jack", "jacky", "tom", "jack")
        .toSet()
        .toList()
        .run(::println)

    listOf("json", "jack", "jacky", "tom", "jack").distinct().run(::println)
}

Operation effect

[json, jack, jacky, tom]
[json, jack, jacky, tom]

As we all know, in the List set, the same data may be inserted, while in the set set, the same key will be directly replaced. Therefore, when duplicate elements appear in the List set, you can convert to set or call the distinct method to remove the duplicate!

4. map set

fun main{
    val map = mapOf("jack" to 20, "tom" to 15, "bob" to 32)
    println(map)
    //Get the corresponding value directly through the key
    map["jack"].run(::println)
    //You can also use getValue
    map.getValue("jack").run(::println)
    //If it is not found, the else closure is returned
    map.getOrElse("aa") { "error" }.run(::println)
    //If not found, the default value is returned
    map.getOrDefault("err", 0).run(::println)
    map.forEach { (key, value) ->
        println("$key,  $value")
    }
    
    //Force to modifiable set
    val mutableMap = map.toMutableMap()
    mutableMap += "jimmy" to 30
    mutableMap["Jimmy"] = 32
    println(mutableMap.getOrPut("jimmy") { 18 })
    println(mutableMap.getOrPut("Rose") { 18 })
    println(mutableMap)
}

Operation effect

{jack=20, tom=15, bob=32}
20
20
error
0
jack,  20
tom,  15
bob,  32

From this result, it can be seen that when the toMutable keyword is used, the read-only set can be forcibly transformed into a modifiable set.

Conclusion

Well, that's the end of this article. I believe the little friends who have read this article know something about the use of collections. In the next article, Kotlin's objects, interfaces and abstract classes will be explained in detail!

Topics: Android kotlin