1. In Kotlin, you can create ArrayList, LinkedList, HashSet, HashMap, etc. in the following ways.
val arrayList = ArrayList<String>() val linkedList = LinkedList<String>() val hashSet = HashSet<String>() val hashMap = HashMap<String,Int>()
2. In addition to the above method, Kotlin also provides other methods to create collections.
1) Create an immutable collection.
The reason why it is immutable is that Kotlin does not provide set, remove, and add methods for collections created in this way. Only get methods are provided.
val list = listOf<Int>(1, 2, 3) val set = setOf<Int>(1, 2, 3) val map = mapOf(1 to "Jack", 2 to "Rose")
2) Create a variable collection. The set created through mutable is a mutable set, which can add set remove operations on the set.
val mutableList = mutableListOf<Int>(1, 2, 3) val mutableSet = mutableSetOf<Int>(1, 2, 3) val mutableMap = mutableMapOf(1 to "Jack", 2 to "Rose")
3. Operation on List.
val list = listOf<Int>(1, 2, 3) //Usually get the way println((1)) //The reason why you can use an array-like method to obtain is because the [] operator overloading is done, as will be discussed later. println(list[1]) //The corner marker will not throw an exception, but will return a null println((3)) //pass? : empty merge operator,? : The value on the left is empty, and the value on the right is returned. println((3) ?: -1) //If the angle marker crosses the boundary, no exception will be thrown. Instead, return the value in the {}lambda expression println((3) { -1 })
1) Usually obtained through get(index):
(1)
2) You can also obtain it like an array by [index]. The reason why it can be obtained using [] is because operator overloading is done.
list[1]
3) getOrNull. If the angle marker crosses the boundary, the exception will not be thrown but will return null. Can it be with? : Used in conjunction with the empty merge operator to return a default value. ? : The value on the left is empty, and the value on the right is returned.
(3)
(3) ?: -1
4) getOrElse receives two parameters, the first index value, and the second lambda expression. If the angle marker goes out of bounds, no exception is thrown, but the value in the lambda expression is returned.
(3) { -1 }
5) Variable sets:
val mutableList = mutableListOf<Int>(1, 2, 3)
set operation
(1,2) mutableList[1] = 2
add operation
(4) mutableList += 5
remove operation: -= operator overloads, the value of the element is deleted
mutableList -= 5 (5)
removeIf receives a lambda expression that can be deleted according to the conditions in the expression.
{ it == 4 }
{ ("H")}
6) Traversal of List collection
You can use in traversal.
forEach receives a lambda expression that will pass elements in the collection.
forEachIndexed receives a lambda expression with two parameters, one is index and the other is the corresponding value
for (item in list) { println(item) } { println(it) } { index, value -> println("$index , $value") }
Deconstruction of collections:
Unwanted elements can be filtered out through _.
val (first:Int,second:Int,third:Int) = listOf<Int>(1, 2, 3)
val (first: Int, _, third: Int) = listOf<Int>(1, 2, 3) println("$first $third")
gather
1) The acquisition method of set is similar to list
val set = setOf<Int>(1, 2, 3) (1) (1) { -1 } (1)
2) Variable set set
val mutableSet = mutableSetOf<Int>(1, 2, 3) mutableSet += 4 mutableSet -= 2 (3) { it==4 }
3) List and set convert each other
Convert list to set to remove duplicate elements. Duplication is not allowed in the set.
val list = (); val set = ();
6. Array type
//ByteArray val byteArray = byteArrayOf(1,2,3) //IntArray val array = intArrayOf(1, 2, 3) //CharArray val charArray = charArrayOf('a','b','c')
gather
1) Creation of map collection
Pari object, the key value is stored
val map = mapOf(Pair(1, "HanMei"), Pair(2, "LiLei"))
to looks like a key, but to is actually a function. This writing method will be discussed later.
val map = mapOf(1 to "HanMei", 2 to "LiLei")
Create variable maps
val mutableMap = mutableMapOf(1 to "HanMei", 2 to "LiLei")
2) Map get operation
println(map[1]) println((1)) println((1, "Default")) println((1) { "default" })
3) Map traversal
{ println("%$ , ${}") } { (key, value) -> println("$key ,$value") }
4) Addition, deletion and modification of variable maps
val mutableMap = mutableMapOf(1 to "HanMei", 2 to "LiLei") mutableMap += 3 to "XiaoHua" (4, "XiaoHua") (5) { "XiaoHua" } val getValue:()->String = { "value" } (5) {getValue()}
This is the end of this article about the introduction to the use of the Kotlin collection List Set Map. For more related Kotlin List Set Map content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!