当前位置:网站首页>Use of kotlin basic common sets list, set and map

Use of kotlin basic common sets list, set and map

2022-06-13 06:24:00 m0_ forty-seven million nine hundred and fourteen thousand one

                

         Set is a kind of data structure we often use , Can be passed as a value to a function , Like other variable types we've learned ,List、Set、Map There are two types of , Read only and variable .

One .List aggregate

1.List Create and get elements

        getOrElse Is a security index value function , He needs two parameters , The first is the index value , The second is to provide default values lambda expression , If the index value does not exist , Can be used instead of exception .

        getOrNull yes Kotlin Another security index value function provided by , It returns null result , Instead of throwing an exception .

fun main() {
    //listOf  Create immutable collections 
    val listOf = listOf("Jason", "Jack", "Jacky")
    // Index value output exceeded lambda Expression content 
     println(listOf.getOrElse(3){"Unknown"})
    // Index value output exceeded null
     println(listOf.getOrNull(3))
}

  

2. variable List aggregate

        stay Kotlin in , The list that supports content modification is called variable list , To create a variable list , have access to mutableListOf function .List You can also use toList and toMutableList Function to dynamically realize the exchange of self reading list and variable list .

fun main() {
    //mutableListOf  Create variable sets 
    val mutableList = mutableListOf("Jason", "Jack", "Jacky")
    // Add an element 
    mutableList.add("XiaoHua")
    // Remove an element 
    mutableList.remove("XiaoHua")
    // Turn immutable 
    val toList = mutableList.toList()
    // Turn variable 
    val toMutableList = toList.toMutableList()
    println(mutableList)
    println(toList)
    println(toMutableList)
}

3.mutator function

        Functions that can modify variable lists have a unified name :mutator function

fun main() {
    //mutableListOf  Create variable sets 
    val mutableList = mutableListOf("Jason", "Jack", "Jacky")
    //+=   And add The same effect 
    mutableList+="Jimmy"
    //-=  And remove  The same effect 
    mutableList-="Jimmy"
    // Remove the qualified parameters 
    mutableList.removeIf {
        it.contains("Jack")
    }
    println(mutableList)
}

4. A collection of traverse

        

fun main() {
    //mutableListOf  Create a read-only collection 
    val list=listOf("Jason", "Jack", "Jacky")
    // Traverse 
    for (s in list) {
        print(s)
    }
    // Traverse 
    list.forEach {
        print(it)
    }
    // Traversal is to get the index 
    list.forEachIndexed { index, item ->
        print("$index, $item")
    }
}

       

5. Deconstruction and de duplication

fun main() {
    //mutableListOf  Create a read-only collection 
    val list=listOf("Jason", "Jack", "Jacky")
    // Structural grammar 
    // hold list Values in the set   Assign to the previous attribute   You can skip the attribute assignment of this position by underlining it 
    val (origin,_,roxy)=list
    print("$origin   $roxy")

    print(listOf("Jason", "Jack", "Jacky", "Jack").distinct())  //list  There are also de duplication methods 
}

Two .Set aggregate

1.Set Create and get elements

        adopt setOf establish Set aggregate , Use elementAt Function to read the elements in the collection .

fun main() {
    // immutable  set aggregate     
    //set Set the automatic de duplication 
    val set = setOf("Jason", "Jack", "Jacky", "Jack")
    //set[1]   There is no such way of writing 
    println(set.elementAt(2))  // Take the element 
}

2. Variable set

        adopt mutableSetOf Create variable set aggregate

fun main() {
    // variable set aggregate 
  //set Set the automatic de duplication                                      
    val mutableSetOf = mutableSetOf("Jason", "Jack", "Jacky", "Jack")
    // Additive elements 
    mutableSetOf+="XiaoHua"
    //Set Set value 
    print(mutableSetOf.elementAt(2))
}

3. Set transformation

fun main() {
    val mutableList = mutableListOf("Jason", "Jack", "Jacky", "Jack")
    // We can go through List Collective transfer Set Collective insect repellent 
    val toList = mutableList.toSet()
    // Turn it back List aggregate 
    val toList1 = toList.toMutableList()
    println(toList1)
}

3、 ... and .Map aggregate

1.Map establish

        to Although it looks like a keyword , But it is actually a special function that omits the period and parameters ,to The function converts its left and right values into a pair of Pair

fun main() {
    val mapOf2 = mapOf("123" to 123, "Jason" to 18, "Jack" to 30)// It's also a way 
    //123 to 123  It's actually   "!23".to(123)   Because in the  infix keyword  . and ()  All omitted 

    val mapOf1 = mapOf(Pair("312", 12312), Pair("123", 124214))// This is also a way to create 
    println(mapOf1)
    println(mapOf2)
}

        

2. Read Map Value

       

fun main() {
    val mapOf2 = mapOf("123" to 123, "Jason" to 18, "Jack" to 30)// It's also a way 
    // These are Map Set value method 
       print( mapOf2.get("Jack"))
       print( mapOf2.getValue("Jack")) // If the value to be taken is null , Abnormal Report 
       print( mapOf2.getOrElse("13"){"Unknown"}) // There is no such value   Put it back lambda The content of the expression 
       print( mapOf2.getOrDefault("13",0)) // There is no such value  , Enter the default value 
       print(mapOf2["123"])
}

3. Traverse

fun main() {
    val mapOf2 = mapOf("123" to 123, "Jason" to 18, "Jack" to 30)// It's also a way 
    // The following is the traversal method 
    mapOf2.forEach {
         print("${it.key}    ${it.value}    ")
    }
                    // Parameters deconstruct 
    mapOf2.forEach { (key, value) ->

          print("$key    $value"   )
    }
}

4. Variable set

        adopt mutableMapOf Create variable Map.

fun main() {
    val mutableMapOf = mutableMapOf("123" to 123, "Jason" to 18, "Jack" to 30)
    mutableMapOf +="12312" to  42124  // Additive elements 
    mutableMapOf["1231241"] = 12213  // Modifying elements 
    mutableMapOf.getOrPut("Rose",{12312}) // Take value first , When there is no current value to get in the set , A default value will be filled in 
    print(mutableMapOf)

        

Next : Kotlin Basics Defining classes and initializing 、 Inherit

      

原网站

版权声明
本文为[m0_ forty-seven million nine hundred and fourteen thousand one ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270555277487.html