使用RXJava 2对数据分组,将元素添加到每个组并输出一个列表 [英] Group data with RXJava 2, add element to each group and output one list

查看:836
本文介绍了使用RXJava 2对数据分组,将元素添加到每个组并输出一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RXJava 2观察器,它发出类型为somethingWithDate的对象.这些对象的属性包含一个Java日期对象以及boolean isHeader.

我需要对这些对象进行以下操作:

  1. 按月和年对它们进行分组(所有日期为2017年1月,所有日期为2016年1月,所有日期为2016年2月,...)
  2. 在组的开头添加标题(也是somethingWithDate)
  3. 按组对彼此进行排序(按日期排序,也可以在组内进行排序,但这不是必需的,标头需要保持在每个组的第一位)
  4. 将所有组中的所有元素合并(?)回到一个元素列表single<List<somethingWithDate>>

这里是一个示例(所有类型为somethingWithDate的对象,但我仅在此处显示重要属性以使其更具可读性):

输入数据:

1.1.2017, 2.1.2017, 1.1.2016, 8.8.2017

结果:

header, 1.1.2016, header, 1.1.2017, 2.1.2017, header, 8.8.2017

我知道1.可以通过使用groupBy()来实现.我还没有找到一种方法来做2.我也知道可能有一个toList()toSortedList()可能用于3.和4.但是我不确定这与groupBy()中的组如何配合使用,以及如何将它们收集到一个列表中.

伪代码:

inputListObserver
    .groupBy(obj.date.year + obj.date.month)
    .map(group -> group.addAtBeginning(headerElement))
    .sortBy(groupKey)
    .mergeToList()
    ...

解决方案

我想我终于找到了答案.使用flatmap(感谢@masps的想法)似乎可以解决问题.这是伪代码:

.groupBy(makeKeyFromMonthAndYear()) // groups everything in months
.sorted(byGroupKey) // sorts the groups
.flatMap( // collect all groups back into one
   .startsWith(generateHeaderElement()) // add a header for each group
   .sorted() // optional, sort the values in a month
)
.toList() // output a Single<List<somethingWithDate>>

I have an RXJava 2 observer that emits objects of type somethingWithDate. These objects have a property that contains a java date object as well as a boolean isHeader.

What I need to do with these objects is:

  1. Group them by month and year (all dates in January of 2017, all dates in January of 2016, all dates in February 2016, ...)
  2. Add a header (also a somethingWithDate) at the start of the group
  3. Sort groups in regard to each other (by date, sorting inside the groups would be nice too but not needed, the header needs to stay at first position in each group)
  4. Merged(?) all elements from all groups back into one list of elements single<List<somethingWithDate>>

Here is an example (all objects of type somethingWithDate, but I will only display the important properties here to make it more readable):

Input Data:

1.1.2017, 2.1.2017, 1.1.2016, 8.8.2017

Result:

header, 1.1.2016, header, 1.1.2017, 2.1.2017, header, 8.8.2017

I know 1. can be achieved by using groupBy(). I haven't found a way to do 2 though. I also know there is a toList() and toSortedList() to maybe use for 3. and 4. but I am unsure how that works with the groups from the groupBy() and how I can collect them back in one list.

Pseudocode:

inputListObserver
    .groupBy(obj.date.year + obj.date.month)
    .map(group -> group.addAtBeginning(headerElement))
    .sortBy(groupKey)
    .mergeToList()
    ...

解决方案

I think I finally found an answer. Using flatmap (thanks for the idea @masps)seems to do the trick. Here is the pseudo code:

.groupBy(makeKeyFromMonthAndYear()) // groups everything in months
.sorted(byGroupKey) // sorts the groups
.flatMap( // collect all groups back into one
   .startsWith(generateHeaderElement()) // add a header for each group
   .sorted() // optional, sort the values in a month
)
.toList() // output a Single<List<somethingWithDate>>

这篇关于使用RXJava 2对数据分组,将元素添加到每个组并输出一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆