如何在Kotlin中的GroupBy操作后获取属于组的值列表?

发布于 2025-02-11 11:16:19 字数 423 浏览 3 评论 0原文

I have the following data:

import kotlin.test.*
import java.util.*


data class Sales(val year: Int, val price: Int)

val myList = listOf(
    Sales(2017, 10),
    Sales(2017, 19),
    Sales(2020, 15),
    Sales(2021, 100),
    Sales(2020, 20),
)

I want to see how to get the list of values for each group.例如,我希望结果为

{2017=[10, 19], 2020=[15, 20], 2021=[100]}

I have the following data:

import kotlin.test.*
import java.util.*


data class Sales(val year: Int, val price: Int)

val myList = listOf(
    Sales(2017, 10),
    Sales(2017, 19),
    Sales(2020, 15),
    Sales(2021, 100),
    Sales(2020, 20),
)

I want to see how to get the list of values for each group. For example, I want the result as

{2017=[10, 19], 2020=[15, 20], 2021=[100]}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆躺在深渊里 2025-02-18 11:16:19

您可以使用以下内容:

import kotlin.test.*
import java.util.*


data class Sales(val year: Int, val price: Int)

val myList = listOf(
    Sales(2017, 10),
    Sales(2017, 19),
    Sales(2020, 15),
    Sales(2021, 100),
    Sales(2020, 20),
)

fun main () {
    val reduced = myList.groupBy({ it.year } , { it.price })
    print(reduced)  //result: {2017=[10, 19], 2020=[15, 20], 2021=[100]}

// further - just for understanding
    val reduced2 = myList.groupBy({ it.year } , { it})
    print(reduced2)  
    //result: 
    //{
        //2017=[Sales(year=2017, price=10), Sales(year=2017, price=19)], 
        //2020=[Sales(year=2020, price=15), Sales(year=2020, price=20)], 
        //2021=[Sales(year=2021, price=100)]
    //}
}

You can use the following:

import kotlin.test.*
import java.util.*


data class Sales(val year: Int, val price: Int)

val myList = listOf(
    Sales(2017, 10),
    Sales(2017, 19),
    Sales(2020, 15),
    Sales(2021, 100),
    Sales(2020, 20),
)

fun main () {
    val reduced = myList.groupBy({ it.year } , { it.price })
    print(reduced)  //result: {2017=[10, 19], 2020=[15, 20], 2021=[100]}

// further - just for understanding
    val reduced2 = myList.groupBy({ it.year } , { it})
    print(reduced2)  
    //result: 
    //{
        //2017=[Sales(year=2017, price=10), Sales(year=2017, price=19)], 
        //2020=[Sales(year=2020, price=15), Sales(year=2020, price=20)], 
        //2021=[Sales(year=2021, price=100)]
    //}
}

在你怀里撒娇 2025-02-18 11:16:19
data class Sales(val year: Int, val price: Int)

val myList = listOf(
  Sales(2017, 10),
  Sales(2017, 19),
  Sales(2020, 15),
  Sales(2021, 100),
  Sales(2020, 20)
)

val result = myList
  .groupBy { it.year }
  .mapValues { (_, value) -> value.map { it.price } }

println(result)   // Output: {2017=[10, 19], 2020=[15, 20], 2021=[100]}
data class Sales(val year: Int, val price: Int)

val myList = listOf(
  Sales(2017, 10),
  Sales(2017, 19),
  Sales(2020, 15),
  Sales(2021, 100),
  Sales(2020, 20)
)

val result = myList
  .groupBy { it.year }
  .mapValues { (_, value) -> value.map { it.price } }

println(result)   // Output: {2017=[10, 19], 2020=[15, 20], 2021=[100]}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文