我有一个数据类报表
data class Report(var id: Int? = null, var user_name: String? = null)
我有一份报告清单。例如:
val reports = listOf(
Report(1, "Mike"),
Report(2, "John"),
Report(3, "Ann"),
Report(4, "Mike"),
Report(5, "Bob"),
Report(6, "Carl"),
Report(7, "Donald"),
Report(8, "John"),
Report(9, "Ann"),
Report(10, "Bob"))
如何使用RxJava将此报告列表转换为{_1}},将由user_name进行处理? id需要的最终变体是这样的:
List<List<Reports>>
答案 0 :(得分:4)
您不需要使用RxJava。 Kotlin就足够了:
val grouped_map = reports.groupBy { it.user_name }
//{Mike=[Report(id=1, user_name=Mike), Report(id=4, user_name=Mike)], John=[Report(id=2, user_name=John), Report(id=8, user_name=John)], Ann=[Report(id=3, user_name=Ann), Report(id=9, user_name=Ann)], Bob=[Report(id=5, user_name=Bob), Report(id=10, user_name=Bob)], Carl=[Report(id=6, user_name=Carl)], Donald=[Report(id=7, user_name=Donald)]}
val reports_grouped_by_user_name = reports.groupBy { it.user_name }.values
//[[Report(id=1, user_name=Mike), Report(id=4, user_name=Mike)], [Report(id=2, user_name=John), Report(id=8, user_name=John)], [Report(id=3, user_name=Ann), Report(id=9, user_name=Ann)], [Report(id=5, user_name=Bob), Report(id=10, user_name=Bob)], [Report(id=6, user_name=Carl)], [Report(id=7, user_name=Donald)]]