根据 Kotlin 中的对象参数从一个对象列表创建多个对象列表
属性的对象列表,如以下:
列表 |
---|
ID:1 |
ID:1 ID:2 |
ID:2 |
ID:2 |
ID:3 |
我想基于该属性创建不同的
列表 | 我有一个带有int | , C |
---|---|---|
ID:1 | ID:2 ID | :3 |
ID:1 | ID:2 |
我找到了一些方法,但我想知道是否有有效的方法,也许使用Kotlin已经提供的某种方法。
I Have a list of object with an Int attribute, like this:
List |
---|
id: 1 |
id: 1 |
id: 2 |
id: 2 |
id: 3 |
and i want to create different list based on that attribute, like this:
List A | List B | List C |
---|---|---|
id: 1 | id: 2 | id: 3 |
id: 1 | id: 2 |
i found some ways to do it but i wanted to know if there was an efficient way, maybe using some method that kotlin already provide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
输出:
Output:
您想要的是GroupBy:
。
What you want is the groupBy: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/group-by.html
It will return a map, that the key is the property value that you used to group by, and the value is the list.