Summary: After the previous article on data types, we’ll talk about collections in Kotlin.
Introduction to the
Kotlin is fully compatible with Java collections, and Kotlin has its own set: mutable and immutable.
Immutable set:
fun main() {
/ * *
* Kotlin immutable set
* All without add and remove methods
*
* In fact, immutable collection, is for the compiler to see. The compiler won't let you write the remove method, and the resulting class files are mapped to Java collections
* Java calls Kotlin's immutable collection, which can be called to remove and add methods
* /
val listOf = listOf< String>("1" , "2", "3")
val setof = setOf< String>("1" , "2", "3")
val map = mapOf< String, Int >(Pair( "1", 1 ), Pair( "2", 2 ))
/ * *
* note:
* Why can we use to to connect? (More on magical infix notation, similar to custom operators)
* /
val mapof = mapOf( "1" to 1 , "2" to 3)
}
Copy the code
Note: Kotlin’s immutable set is defined with a fixed number of elements and contents. This scoping is only available in the Kotlin language (editor limited). If Java calls an immutable collection in Kotlin, this limitation is broken. (Essentially a Java collection)
Mutable combination: Elements can be added after creation.
fun main() {
/ * *
* Mutable sets in Kotlin
* Finally also maps to Java as a collection
* /
val list = mutableListOf<String>("1", "2", "3")
val mutableMapOf = mutableMapOf(1 to 2, 2 to 3)
val mutableSetOf = mutableSetOf(1 , 3)
/ * *
Of course, Kotlin can also use Java's collection definition directly
* /
val javaList = ArrayList<String>()
val javaMap = HashMap<String, Int>()
val javaSet = HashSet<String>()
}
Copy the code
Encapsulation of the Kotlin collection method
Kotlin has extended a number of convenient action set methods for us, so let’s take a look at some of the common ones.
The List collection
The values: Can be obtained by index
fun main() {
val javaList = ArrayList<String>()
/ * *
* Direct access through indexes
* /
val s = javaList[0]
}
Copy the code
Take the interval: Remember Kotlin’s particular interval? When we want to fetch a range from a list, we use the slice method.
fun main() {
val javaList = ArrayList
()
/ * *
* Select the interval, of course, you can also use the half-open interval until
* /
val result = javaList.slice( 0.. 2)
}
Copy the code
The List collection just adds and subtractsKotlin List is now operable. (Think about it, guys. How?)
operation | explain |
---|---|
list1 + list2 | The final result is to merge the two sets |
list1 – list2 | The final result, the difference between list1 and list2 |
/ * *
* Add two sets to get a third set.
* 2 sets are subtracted, the set on the left deletes the elements that exist in the set on the right
* /
fun main() {
/ * *
* Here I purposely created 2 collections of different types
* /
val list = mutableListOf<String>("1", "2", "3")
val javaList = ArrayList<String>()
/ * *
* 1 for mutableList and 1 for ArrayList
* The discovery that they can be added up proves that they are indissolubly related
* /
val listAdd = list + javaList
}
Copy the code
Traversal, transformation, filteringKotlin provides many rXJava-like operators.
fun main() {
val javaList = ArrayList<String>()
/ * *
* Data conversion
* /
javaList.map {
}
/ * *
* filter
* /
javaList.filter {
}
/ * *
* flatMap conversion
* /
javaList.flatMap {
}
/ * *
* traversal
* /
javaList.forEach {
}
/ * *
* Index + value traversal
* /
javaList.forEachIndexed { index, s ->
}
}
Copy the code
Remove elementsKotlin’s remove method removes the specified element. RemoveAt removes the specified element by index. RemoveIf removes the specified element conditionally.
fun main() {
val list= mutableListOf<String>()
/ * *
* Object deletion
* /
list.remove("")
/ * *
* Index drop
* /
list.removeAt(0)
/ * *
* Conditional delete
* /
list.removeIf {
}
}
Copy the code
Map collections
The Map collection is a commonly used collection in Java. But I have to complain that Map collection traversal in Java is disgusting. Kotlin made a big change, so let’s take a look.
Add elements: Elements can be added using [], a syntax similar to array assignment (the value of key is also supported).
fun main() {
/ * *
* Create a mutable Map and add 2 elements
* Note that the increment element is in the form of a key to value, which will be explained later
* /
Val mutableMap = mutableMapOf<String, Int>(" a "to 18," b "to 18)
/ * *
* New way to add elements, add an element with key= value=12
* /
MutableMap [" mucha "] = 12
}
Copy the code
Remove elements
fun main() {
Val mutableMap = mutableMapOf<String, Int>(" a "to 18," b "to 18)
MutableMap. Remove (" text ")
}
Copy the code
Traversal, transformation, filtering
fun main() {
Val mutableMap = mutableMapOf<String, Int>(" a "to 18," b "to 18)
/ * *
* map transformation
* /
mutableMap.map {
}
/ * *
* filter
* /
mutableMap.filter {
}
/ * *
* traversal
* /
mutableMap.forEach { key, value ->
}
/ * *
* in traverse
* /
for (mutableEntry in mutableMap) {
}
/ * *
* This type of traversal is the same as in traversal above
* How can I write this? This will be supplemented later by the overloaded operators
* /
for ((value, key) in mutableMap) {
}
}
Copy the code
conclusion
Kotlin provides a number of powerful helper methods for collections that are simple to implement. (Both encapsulate collections. All of this is thanks to Koltin’s extension functions. Look forward to our next article!) .
Reference content:
- Reference – Kotlin assigned https://www.kotlincn.net/docs/reference/ language
Recommended reading
–END–
Identify the QR code, follow us