Kotlin笔记函数式编程API(十)

Kotlin笔记函数式编程API(十)

Kotlin笔记数据类型(一)
Kotlin笔记字符串(二)
Kotlin笔记运算符(三)
Kotlin笔记函数(四)
Kotlin笔记面向对象(五)
Kotlin笔记继承、抽象类、接口(六)
Kotlin笔记高阶函数(七)
Kotlin笔记泛型(八)
Kotlin 笔记 数据容器(九)



一、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

二、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}")
    }
    
    

三、过滤函数

1.返回集合类型

1、filter

过滤操作使用filter函数,它可以对Collection集合、Map集合或数组元素进行过滤,
Collection集合和数组返回的是一个List集合,Map集合返回的还是一个Map集合



    users.filter {
    
    
        it.name.startsWith("T",ignoreCase = false)
    }
        .filter {
    
    
            it.name==="Tony"
        }
        .forEach {
    
    
            println(it.name)
        }
        
        
        
Tony        
        

2、drop

返回不包括前n个元素的List集合


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

返回前n个元素List集合。

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

返回非空元素List集合。注意Array数组是对象数组不能
是IntArray和FloatArray等基本数据类型数组

5、takeLast

返回后n个元素List集合。


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

返回指定索引的元素List集合。

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

与filter相反,过滤出不符合条件的数据。

2、返回单个元素

1、find

返回符合条件的第一个元素,如果没有符合条件的元素,
则返回空值

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

返回符合条件的最后一个元素,如果没有符合条件的元
素,则返回空值。

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

返回第一个元素,函数没有参数

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

返回第最后一个元素,函数没有参数

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{}

返回符合条件的第一个元素,函数有一个参数。如果没有
符合条件的元素,抛出异常

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{}

返回符合条件的最后一个元素,函数有一个参数。如果没
有符合条件的元素,抛出异常

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

返回符合条件的第一个元素,函数有一个参数。如果没有
符合条件的元素,则返回空值

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

返回符合条件的最后一个元素,函数有一个参数。如果没
有符合条件的元素,则返回空值

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

四、映射

  • mapNotNull 返回一个List集合,该List集合包含对原始集合中非空元素进行
    转换后结果。注意Array数组是对象数组不能是IntArray和
    FloatArray等基本数据类型数组
  • mapIndexed 返回一个List集合,该List集合包含对原始集合中每个元素进行
    转换后结果和它们的索引
  • flatMap 扁平化映射,可以将多维数组或集合变换为一维集合。
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]

}

五、排序函数

  • sorted 返回集合或数组自身类型 升序
  • sortedBy 指定表达式计算之后再进行升序排序
  • sortedDescending 降序
  • sortedByDescending 指定表达式计算之后再进行降序排序
  • reversed 将原始倒置
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]
}

猜你喜欢

转载自blog.csdn.net/baidu_31956557/article/details/109297759