当前位置:网站首页>Common higher-order functions of filter, find, any, map, flatMap, takeWhile, reduce, groupBy in Kotlin series

Common higher-order functions of filter, find, any, map, flatMap, takeWhile, reduce, groupBy in Kotlin series

2022-09-23 08:36:14Little M's Memories


        Higher-order functions are multi-order maps.A higher-order function takes another function as its input parameter,It is also possible to return a function as output.

高阶函数:filter

是一个过滤器,Ability to filter out data that meets the conditions by various conditions.

val numbers1 = listOf<Int>(213, 4, 534, 646, 757, 8, 97, 9);
var result = numbers1.filter {
     
    it < 10
}

结果:4,8,9

高阶函数:any

Used to determine whether there is data that meets the conditions in the data,The returned result is a boolean value.true或者false

val numbers1 = listOf<String>("123", "田园犬", "345", "收到");
  println(dogdatabase.any{
    it.kind=="田园犬"}) 
  结果:true

高阶函数:maxByOrNull

求最大值

val numbers2 = listOf<Int>(213, 4, 534, 646, 757, 8, 97, 9);
numbers1.maxByOrNull {
     it }
结果:757 (最大值)

高阶函数:minByOrNull

求最小值

val numbers2 = listOf<Int>(213, 4, 534, 646, 757, 8, 97, 9);
numbers1.minByOrNull {
     it }
结果:4(最小值)

高阶函数:map

Traverse a collection,Then generate new collections according to your own rules.

val arr = intArrayOf(1, 2, 4, 6)
val newArr = arr.map {
     (it * 2).toString()  }

val newArr1 = arr.map {
    
    if (it == 2) {
    
        it * 2
    }
}

高阶函数:flatMap

nested traversal collection,生成新的集合.

val list = mutableListOf(
    arrayOf("华晨宇", "张碧晨", "邓紫棋"),
    arrayOf("何老师", "汪涵"),
    arrayOf("谢娜", "张杰", "汪诗诗"),
)
val flatList = list.flatMap {
    
    it.map {
     name -> "大明星:$name" }
}
flatList.forEach(::println)

高阶函数:reduce

Applies the provided operation to the elements of the collection and returns the accumulated result
从第一个元素开始累加值,and applies the operation to the current accumulator value and each element from left to right.

val list = listOf(1, 2, 5, 2)
list.reduce {
     acc, i ->
    println("acc:$acc,i:$i")
    acc + i
}

高阶函数:fold

这个函数和reduce类似,However, it can set an initial value

val list = listOf(1, 2, 5, 2)
list.fold(2) {
     acc, i ->  //初始值为2
    println("acc:$acc,i:$i")
    acc + i
}
joinToString
val result = (0..6).joinToString(",")
println("result:$result")
结果: "0,1,2,3,4,5,6"

val number = listOf("one", "two", "three", "four")
number.joinToString(separator = " | ", prefix = "start:", postfix = ":end")
结果:start:one|two|three|four|:end

高阶函数:takeWhile

The first one that doesn't meet the conditions ends,Leave the previous one and return it as a new set.

val list = listOf(
    UserEntity(100, "华晨宇", 30),
    UserEntity(101, "张碧晨", 31),
    UserEntity(103, "邓紫棋", 29),
    UserEntity(103, "李宇春", 32),
)
val result = list.takeWhile {
    
    it.age >= 30
}
println("result:$result")
//result:[userName:华晨宇,userId:100,age:30, userName:张碧晨,userId:101,age:31]

高阶函数:count

Counts the number of eligible elements in a list

val len1 = "Meng qingLong".count()
val len2 = "Meng qingLong".count(){
       //Counts matches in the string'n'的数量
    it == 'n'
}

高阶函数:find

Finds matching criteria and returns the first data matching the criteria.他跟filter的区别就是.filterWhat is returned is the result set.而findReturns the first one found.

val listOf = listOf<String>("a", "b", "c")
val find: String? = listOf.find {
     it == "a" }
//找到"a" returns the current oneString类型的"a"

高阶函数:groupBy

分组.即根据条件把集合拆分为为一个Map<K,List>类型的集合

fun main(string: Array<String>) {
    
    val s = listOf("china","beijing","wuhan","guangzhou","Changsha","wuhu")
    val n1 = s.groupBy {
    
        //Group by capitalization
        it.first().uppercase()   
        //返回成Map的Key. Elements that satisfy the conditions are entered accordinglyMap集合中的Value,List列表中
    }
    println(n1)
}
//输出
{
    C=[china, Changsha], B=[beijing], W=[wuhan, wuhu], G=[guangzhou]}
原网站

版权声明
本文为[Little M's Memories]所创,转载请带上原文链接,感谢
https://chowdera.com/2022/266/202209230835180929.html

随机推荐