Java 在多个文件上使用groupingBy条件后获取计数
我想在多个字段上使用groupingBy条件后获得userId的计数 我的模型课Java 在多个文件上使用groupingBy条件后获取计数,java,java-8,java-stream,Java,Java 8,Java Stream,我想在多个字段上使用groupingBy条件后获得userId的计数 我的模型课 public class ModelClass{ int bussAssId; int trackingId; int userId; int month; String day; int hour; String deviceType ; //Setter and Getters } 在使用JParepositoy的服务类中,我将获得数据列表。此处usedId可以相同,但trackingId将是唯一的 Li
public class ModelClass{
int bussAssId;
int trackingId;
int userId;
int month;
String day;
int hour;
String deviceType ;
//Setter and Getters
}
在使用JParepositoy的服务类中,我将获得数据列表。此处usedId可以相同,但trackingId将是唯一的
List<ModelClass> sqlModelList=postgreSQLRepository.findByBussAssId(bussAssId);
我使用以下代码进行分组,但不了解如何排序
Map<Integer,Map<String,Map<Integer,Map<String,List<PostgreSQLModel>>>>> device=sqlModelList.stream().collect(Collectors.groupingBy(p->p.getMonth(),Collectors.groupingBy(p->p.getDay(),Collectors.groupingBy(p->p.getHour(),Collectors.groupingBy(p->p.getPrimaryKeys().getDeviceType())))));
Map device=sqlmodelist.stream().collect(Collectors.groupingBy(p->p.getMonth()、Collectors.groupingBy(p->p.getDay()、Collectors.groupingBy(p->p.getPrimaryKeys()、getDeviceType()));
输出应如下所示:
您需要首先对数据进行分组,然后对其进行排序:
List<GroupModelClass> devices = sqlModelList.stream()
.collect(Collectors.groupingBy(GroupModelClass::new, Collectors.counting()))
.entrySet().stream()
.map(e -> e.getKey().setCount(e.getValue()))
.sorted(Comparator.comparingLong(GroupModelClass::getCount).reversed())
.collect(Collectors.toList());
结果将是一个按计数降序排列的
组模型列表。您需要先对数据进行分组,然后再对其进行排序:
List<GroupModelClass> devices = sqlModelList.stream()
.collect(Collectors.groupingBy(GroupModelClass::new, Collectors.counting()))
.entrySet().stream()
.map(e -> e.getKey().setCount(e.getValue()))
.sorted(Comparator.comparingLong(GroupModelClass::getCount).reversed())
.collect(Collectors.toList());
结果将是一个按计数降序排列的groupModels列表。Map
如果您的类型看起来像这样,则说明您犯了严重错误。@Michael我想对多个字段使用groupingBy如何实现这一点?使用list.stream().sorted(Comparator)进行排序
请允许我建议您使用内置的枚举Month
和DayOfWeek
而不是int
和字符串。Map device=sqlmodelsist.stream().collect(Collectors.groupingBy(p->Arrays.asList(p.getMonth(),p.getDay(),p.getHour(),p.getPrimaryKeys().getDeviceType()),Collectors.counting())
,但最好用具有所需属性的专用键类型(以及适当的hashCode
/等于实现)替换List
。然后,通过映射将条目转换为结果记录类型(保存键属性和计数),对它们进行排序并收集到列表中map
,如果您的类型与此类似,你做错了严重的事情。@Michael,我想对多个字段使用groupingBy,如何实现这一点?使用list.stream().sorted(Comparator)
允许我建议你使用内置的枚举Month
和DayOfWeek
而不是int
和字符串。Map device=sqlmodelslist.stream().collect(collector.groupingBy(p->Arrays.asList(p.getMonth(),p.getDay(),p.getHour(),p.getPrimaryKeys().getDeviceType()),collector.counting());
,但最好用具有所需属性的专用键类型(以及适当的hashCode
/等于实现)替换。然后,在地图上流动,将条目转换为结果记录类型(保存键属性和计数),对它们进行排序并收集到列表中。
public static class GroupModelClass {
private int month;
private String day;
private int hour;
private String deviceType;
private long count;
public GroupModelClass(ModelClass model) {
this.month = model.getMonth();
this.day = model.getDay();
this.hour = model.getHour();
this.deviceType = model.getDeviceType();
}
public GroupModelClass setCount(long count) {
this.count = count;
return this;
}
// getter
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GroupModelClass that = (GroupModelClass) o;
return month == that.month &&
hour == that.hour &&
count == that.count &&
Objects.equals(day, that.day) &&
Objects.equals(deviceType, that.deviceType);
}
}