Kotlin Notes Functional Programming API (10)
Kotlin note data type (1) Kotlin note string (2) Kotlin note operator (3) Kotlin note function (4) Kotlin note object-oriented (5) Kotlin note inheritance, abstract class, interface (6) Kotlin note high-order function (7) Kotlin note generics (8) Kotlin note data container (9)
Article directory
one, foreach
var strArr= arrayOf("name","age")
strArr.forEach {
println(it)
}
var listName= arrayListOf("tom","Lili")
listName.forEach {
println(it)
}
var mapkv= hashSetOf(1 to "tom",2 to "lili")
mapkv.forEach {
println("key=${it.first }, value=${it.second}")
}
name
age
tom
Lili
key=1, value=tom
key=2, value=lili
Two, forEachIndexed
val strArray = arrayOf("张三", "李四", "王五", "董六")//创建字符串数组
val set = setOf(1, 3, 34, 54, 75)
strArray.forEachIndexed {
index, s ->
println("index:${index} s:${s}")
}
set.forEachIndexed {
index, s ->
println("index:${index} s:${s}")
}
3. Filter function
1. Return collection type
1、filter
The filtering operation uses the filter function, which can filter Collection collections, Map collections or array elements.
Collection collections and arrays return a List collection, and Map collections return a Map collection.
users.filter {
it.name.startsWith("T",ignoreCase = false)
}
.filter {
it.name==="Tony"
}
.forEach {
println(it.name)
}
Tony
2、drop
Returns a List collection excluding the first n elements
val map = mapOf(102 to "张三", 105 to "李四", 109 to "王五")
val array = intArrayOf(1, 3, 34, 54, 75)
val charList = listOf("A", null, "B", "C")
println(array.takeLast(3)) //[34, 54, 75]
3、take
Returns the first n elements of the List collection.
val map = mapOf(102 to "张三", 105 to "李四", 109 to "王五")
val array = intArrayOf(1, 3, 34, 54, 75)
val charList = listOf("A", null, "B", "C")
println(array.take(3)) //[1, 3, 34]
4、filterNotNull
Returns a List collection of non-empty elements. Note that the Array array is an object array and cannot
be an array of basic data types such as IntArray and FloatArray
5、takeLast
Return the List collection of the last n elements.
val map = mapOf(102 to "张三", 105 to "李四", 109 to "王五")
val array = intArrayOf(1, 3, 34, 54, 75)
val charList = listOf("A", null, "B", "C")
println(array.takeLast(3)) //[34, 54, 75]
6、slice
Returns the List collection of elements at the specified index.
val map = mapOf(102 to "张三", 105 to "李四", 109 to "王五")
val array = intArrayOf(1, 3, 34, 54, 75)
val charList = listOf("A", null, "B", "C")
println(array.slice(listOf(0, 2))) //[1, 34] ①
7、filterNot
Contrary to filter, filter out data that does not meet the conditions.
2. Return a single element
1、find
Returns the first element that meets the condition, or
null if there is no element that meets the condition
val map = mapOf(102 to "张三", 105 to "李四", 109 to "王五")
val array = intArrayOf(1, 3, 34, 54, 75)
val charList = listOf("A", null, "B", "C")
println(array.find {
it > 10 }) //34
2、findLast
Returns the last element that meets the condition, or
null if there is no element that meets the condition.
val map = mapOf(102 to "张三", 105 to "李四", 109 to "王五")
val array = intArrayOf(1, 3, 34, 54, 75)
val charList = listOf("A", null, "B", "C")
println(array.findLast {
it < -1 }) //null
3、first
Returns the first element, the function has no parameters
val map = mapOf(102 to "张三", 105 to "李四", 109 to "王五")
val array = intArrayOf(1, 3, 34, 54, 75)
val charList = listOf("A", null, "B", "C")
println(array.first()) //1 ②
4、last
Returns the last element, the function has no parameters
val map = mapOf(102 to "张三", 105 to "李四", 109 to "王五")
val array = intArrayOf(1, 3, 34, 54, 75)
val charList = listOf("A", null, "B", "C")
println(array.last()) //75
5、first{}
Returns the first element that meets the criteria, the function has one argument. If there is no
matching element, an exception is thrown
val map = mapOf(102 to "张三", 105 to "李四", 109 to "王五")
val array = intArrayOf(1, 3, 34, 54, 75)
val charList = listOf("A", null, "B", "C")
println(array.first {
it > 10 }) //34 ③
6、last{}
Returns the last element that meets the criteria, the function has one argument. If there
is no eligible element, an exception is thrown
val map = mapOf(102 to "张三", 105 to "李四", 109 to "王五")
val array = intArrayOf(1, 3, 34, 54, 75)
val charList = listOf("A", null, "B", "C")
println(array.last {
it > 10 }) //75
7、firstOrNull
Returns the first element that meets the criteria, the function has one argument. Returns null if there is no
matching element
val map = mapOf(102 to "张三", 105 to "李四", 109 to "王五")
val array = intArrayOf(1, 3, 34, 54, 75)
val charList = listOf("A", null, "B", "C")
println(array.firstOrNull {
it > 100 }) //null ④
8、lastOrNull
Returns the last element that meets the criteria, the function has one argument. Returns null if there is no
matching element
val map = mapOf(102 to "张三", 105 to "李四", 109 to "王五")
val array = intArrayOf(1, 3, 34, 54, 75)
val charList = listOf("A", null, "B", "C")
println(array.lastOrNull {
it > 100 }) //null
4. Mapping
- mapNotNull returns a List collection, which contains
the converted result of the non-null elements in the original collection.
Note that the Array array is an object array and cannot be an array of basic data types such as IntArray and FloatArray - mapIndexed returns a List collection that contains
the converted results and their indexes for each element in the original collection - flatMap is a flat map, which can transform multidimensional arrays or collections into one-dimensional collections.
fun main(args: Array<String>) {
val set = setOf(1, 3, 34, 54, 75)
val charList = listOf("A", null, "b", "C")
println(charList.mapNotNull {
it } //[A, b, C] ①
.map {
it.toLowerCase() }) //[a, b, c] ②
println(set.mapIndexed {
index, s -> index + s }) // [1, 4, 36, 57, 79] ③
val datas = listOf(listOf(10, 20), listOf(20, 40))
val flatMapList = datas.flatMap {
e -> e.map {
it * 10 } }
println(flatMapList)//[100, 200, 200, 400]
}
5. Sorting function
- sorted returns the collection or array itself in ascending order
- sortedBy Sort in ascending order after the specified expression is calculated
- sortedDescending 降序
- sortedByDescending sorts in descending order after the specified expression is calculated
- reversed reverses the original
fun main(args: Array<String>) {
val set = setOf(1, -3, 34, -54, 75)
//升序
println(set.sorted())//[-54, -3, 1, 34, 75]
println("Users升序输出:")
users.sortedBy {
it.name }.forEach {
println(it) } ①
//降序
println(set.sortedDescending())//[75, 34, 1, -3, -54]
println("Users降序输出:")
users.sortedByDescending {
it.name }.forEach {
println(it) } ②
//倒置
println(set.reversed())//[75, -54, 34, -3, 1]
}