Kotlin学习 03 Stream 简化版的操作

Stream 操作 Java 和 Kotlin 的对比

先写一个数据类

/**
 * @Description  :  数据类
 * @Date  :2023/5/25
 * @Author  :Liu Siyuan
 * @Project  : kotlinLearning
 * @Version  1.0.0
 */
data class Fruits(var id:Int?, var name:String?, var price: Double?, var num:Int? ) {
    constructor(): this(null, null, null, null)

    fun getMe() = this

    fun getMoney() = price!! * num!!
}
View Code

再开始操作:

1、分组

// 分组的两种实现方式
    fun groupBy() {
        // 分组转Map
        appleList.also { println("Kotlin Stream groupBy") }.stream().collect(Collectors.groupingBy(Fruits::id)).forEach { (id, fruit) ->
            println("\t水果ID $id, 详情:$fruit")
        }
        appleList.groupBy(Fruits::id).forEach{ (id, fruit) ->
            println("\t水果ID $id, 详情:$fruit")
        }
        // 抽取属性值分组转Map
        appleList.also { println("Kotlin Stream groupBy") }.groupBy(Fruits::price, Fruits::name).forEach { (price, name) ->
            println("\t水果价格 $price, 名称:$name")
        }
    }
View Code

2、LIst 转 Map

fun list2Map() {
        appleList.also { println("Kotlin Stream list2Map like Java ") }.stream().collect(Collectors.toMap(Fruits::price, Fruits::getMe))
            .forEach { (price, fruit) ->
                println("\t水果价格: $price, 详情:$fruit")
        }

        // 根据属性构造MAP集合,属性作为KEY使用(如果重复,后面会覆盖前面)
        appleList.also { println("Kotlin Stream list2Map") }.associateBy(Fruits::id)
            .forEach { (price, fruit) ->
                println("\t水果价格: $price, 详情:$fruit")
        }
    }
View Code

3、filter

fun filter() {
        appleList.also { println("Kotlin Stream filter like Java stream ") }.stream().filter{ it.price!! > 5}.collect(Collectors.toList())
            .forEach { println("\t\t$it") }
        println()
        appleList.also { println("Kotlin Stream filter use Kotlin simplified") }.filter { it.price!! > 5 }
            .forEach {println("\t\t$it")}
    }
View Code

4、sum

 fun sum() {
        var sum1 = appleList.also { println("Kotlin Stream sum like Java stream ") }.stream().collect(Collectors.summingDouble(Fruits::getMoney))
        println(sum1)

        //kotlin
        println("\t\t总金额为:${appleList.also { println("Kotlin Stream sum use Kotlin simplified:") }.sumOf { it.getMoney() }}元")

        var sum2 = appleList.also { println("Kotlin Stream sum use Kotlin simplified:")}.sumOf { it.getMoney() }
        println(sum2)
    }
View Code

5、Max Min

    fun maxAndMin() {
        val max = appleList.also { println("Kotlin Stream max like Java stream ") }.stream().max{ a1, a2 -> if (a1.price!! > a2.price!!) 1 else -1 }.get()
        val min = appleList.also { println("Kotlin Stream min like Java stream ") }.stream().min{ a1, a2 -> if (a1.price!! > a2.price!!) 1 else -1 }.get()
        println("最大值 $max, 最小值 $min")

        println()
        val max1 = appleList.also { println("Kotlin Stream max use Kotlin simplified ") }.maxWith{a1, a2 -> if (a1.price!! > a2.price!!) 1 else -1}
        val min1 = appleList.also { println("Kotlin Stream min use Kotlin simplified ") }.minWith(){a1, a2 -> if (a1.price!! > a2.price!!) 1 else -1}
        println("最大值 $max1, 最小值 $min1")
    }

 



posted @ 2023-05-25 17:21  Liu Siyuan  阅读(108)  评论(0编辑  收藏  举报