RxJava Android 使用 GroupBy 列表到列表列表
RxJava Android List to List of Lists using GroupBy
我有一个数据class举报
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 将此报告列表转换为 List<List<Reports>>
,user_name 将对其进行摸索?
id 需要的最终变体是这样的:
val reports_grouped_by_user_name = listOf(
listOf(Report(1, "Mike"),Report(4, "Mike")),
listOf(Report(2, "John"),Report(8, "John")),
listOf(Report(3, "Ann"),Report(9, "Ann")),
listOf(Report(5, "Bob"),Report(10, "Bob")),
listOf(Report(6, "Carl")),
listOf(Report(7, "Donald")),
listOf(Report(10, "Bob")))
您不需要为此使用 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)]]
我有一个数据class举报
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 将此报告列表转换为 List<List<Reports>>
,user_name 将对其进行摸索?
id 需要的最终变体是这样的:
val reports_grouped_by_user_name = listOf(
listOf(Report(1, "Mike"),Report(4, "Mike")),
listOf(Report(2, "John"),Report(8, "John")),
listOf(Report(3, "Ann"),Report(9, "Ann")),
listOf(Report(5, "Bob"),Report(10, "Bob")),
listOf(Report(6, "Carl")),
listOf(Report(7, "Donald")),
listOf(Report(10, "Bob")))
您不需要为此使用 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)]]