我有一份餐厅预订列表。 我想按年份的日期分组,并按当天的时间对它们进行排序。 如何使用rxjava实现?
List reservations;
class Reservation {
public String guestName;
public long time; //time in milliseconds
}
输出
- 2015年3月5日
- 20:00预订
- 22:00预订
- 2015年3月8日
- 16:00预订
我有一份餐厅预订列表。 我想按年份的日期分组,并按当天的时间对它们进行排序。 如何使用rxjava实现?
List reservations;
class Reservation {
public String guestName;
public long time; //time in milliseconds
}
输出
toSortedList
),然后在flatMap
中执行手动分组,并输出一个Observable<List<Reservation>>
,从而得到每一天的预订信息。Observable.from(reservations)
.toSortedList(new Func2<Reservation, Reservation, Integer>() {
@Override
public Integer call(Reservation reservation, Reservation reservation2) {
return Long.compare(reservation.time, reservation2.time);
}
})
.flatMap(new Func1<List<Reservation>, Observable<List<Reservation>>>() {
@Override
public Observable<List<Reservation>> call(List<Reservation> reservations) {
List<List<Reservation>> allDays = new ArrayList<>();
List<Reservation> singleDay = new ArrayList<>();
Reservation lastReservation = null;
for (Reservation reservation : reservations) {
if (differentDays(reservation, lastReservation)) {
allDays.add(singleDay);
singleDay = new ArrayList<>();
}
singleDay.add(reservation);
lastReservation = reservation;
}
return Observable.from(allDays);
}
})
.subscribe(new Action1<List<Reservation>>() {
@Override
public void call(List<Reservation> oneDaysReservations) {
// You will get each days reservations, in order, in here to do with as you please.
}
});
differentDays
方法作为读者的练习。由于您已拥有完整的预订列表(即List<Reservation>
,而不是Observable<Reservation>
),因此在此处您不需要使用RxJava——您可以使用Java 8 stream/collections API完成所需操作:
Map<Calendar, List<Reservation>> grouped = reservations
.stream()
.collect(Collectors.groupingBy(x -> {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(x.time);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal;
}));
正如你所看到的,它所做的就是按一年中的日期进行groupingBy
。如果您的初始数据使用Calendar
而不是long
作为时间戳,那么这看起来甚至更简单,像这样:
Map<Calendar, List<Reservation>> grouped = reservations
.stream()
.collect(Collectors.groupingBy(x -> x.time.get(Calendar.DAY_OF_YEAR)));