更新时间:2023-02-14 13:00:01
查看 group by 功能.
以下是任何好奇的人的示例:
Here's the example for anyone who's curious:
class DateModel implements Comparable<DateModel>{
Integer date;
Integer value;
public DateModel(int date, int value){
this.date = date;
this.value = value;
}
@Override
public int compareTo(DateModel o) {
return value.compareTo(o.value);
}
}
然后,如果我们必须聚合这些模型对象的列表:
And then if we have to aggregate a list of these model objects:
// example list
List<DateModel> dateList = Arrays.asList(
new DateModel(18,1),
new DateModel(18,2),
new DateModel(18,3),
new DateModel(19,1),
new DateModel(19,2),
new DateModel(19,3),
new DateModel(19,4)
);
// the following observable will give you an emission for every grouping
// for the example data above, you should get two emissions (group 18 and 19)
Observable<PriorityQueue<DateModel>> observable =
Observable.from(dateList)
.groupBy(dateModel -> dateModel.date)
.flatMap(groups -> groups.collect(PriorityQueue::new, PriorityQueue::add));
PriorityQueue
只是用于收集的结构示例.如果您从队列中弹出,您将得到 18-1、18-2、18-3 等(按照您要求的顺序).您可以使用不同的结构来仅找到最大值 &分钟
PriorityQueue
was just an example of the structure used for collecting. If you pop from queue, you'll get 18-1, 18-2, 18-3 etc (in the order you asked). You can use a different structure for the purposes of only finding the max & min.