流组传入排序事件



我有一系列事件,每个事件包含时间戳、设备、序列号和度量。

class Event {
private String device;
private String description;
private String serialnumber;
private Measurement measurement;
}
class Measurement {
private LocalDateTime timestamp;
private int value;
}

我有这些事件的一个流,我想把它们聚合成一个更简单的结构,去掉序列号,然后按设备分组,然后按时间戳和值排序测量值。

{device: "device_1", description: "first device", serialnumber: "1", measurement: { timestamp: 2022-04-23T18:20:22Z, value: 180}}
{device: "device_2", description: "second device", serialnumber: "2", measurement: { timestamp: 2022-04-23T18:20:28Z, value: 120}}
{device: "device_2", description: "second device", serialnumber: "2", measurement: { timestamp: 2022-04-23T18:20:20Z, value: 160}}
{device: "device_1", description: "first device", serialnumber: "1", measurement: { timestamp: 2022-04-23T18:20:22Z, value: 170}}
[
{
device: "device_1",
description: "first device",
measurements: [
{ timestamp: 2022-04-23T18:20:22Z, value: 170},
{ timestamp: 2022-04-23T18:20:22Z, value: 180}
]
},
{
device: "device_2",
description: "second device",
measurements: [
{ timestamp: 2022-04-23T18:20:20Z, value: 160},
{ timestamp: 2022-04-23T18:20:28Z, value: 120}
]
}
]

我已经设法通过创建"builder"来获得所需的格式。类,您可以在其中插入事件,然后以正确的格式/顺序处理并添加到数据成员中。然而,我认为如果不使用另一个额外的类,而是使用groupingBytoMap(以及其他?)流方法,以某种方式在运行中实现这一点会更好。

我已经提出了这个解决方案,假设每个设备不能有多个测量具有相同的时间戳和值。

我知道这可能是一个限制,但是因为我看不到任何排序收集器收集的子集的方法,我决定使用TreeSet来存储分组的测量值,以便它们将根据其自然顺序进行排序。

class Event {
private String device;
private String description;
private String serialnumber;
private Measurement measurement;
public Event(String device, String description, String serialnumber, Measurement measurement) {
this.device = device;
this.description = description;
this.serialnumber = serialnumber;
this.measurement = measurement;
}
//... getters and setters ...
}
class Measurement implements Comparable<Measurement> {
private LocalDateTime timestamp;
private int value;
public Measurement(LocalDateTime timestamp, int value) {
this.timestamp = timestamp;
this.value = value;
}
//... getters and setters ...
//Redefinition of the natural ordering by timestamp and value
@Override
public int compareTo(Measurement m) {
Comparator<Measurement> cmp = Comparator.comparing(Measurement::getTimestamp).thenComparing(Measurement::getValue);
return cmp.compare(this, m);
}
public String toString(){
return String.format("%s - %d", timestamp, value);
}
}
public class Test {
public static void main(String[] args) {
List<Event> dataSource = new ArrayList<>(List.of(
new Event("device_1", "first device", "1", new Measurement(LocalDateTime.parse("2022-04-23T18:20:22"), 180)),
new Event("device_2", "second device", "2", new Measurement(LocalDateTime.parse("2022-04-23T18:20:28"), 120)),
new Event("device_2", "second device", "2", new Measurement(LocalDateTime.parse("2022-04-23T18:20:20"), 160)),
new Event("device_1", "first device", "1", new Measurement(LocalDateTime.parse("2022-04-23T18:20:22"), 170))));
Map<String, TreeSet<Measurement>> map = dataSource.stream()
.collect(Collectors.groupingBy(Event::getDevice, Collectors.mapping(Event::getMeasurement, Collectors.toCollection(TreeSet::new))));
for (String key: map.keySet()){
System.out.printf("%s => %s%n", key, map.get(key));
}
}
}

您可以使用groupingBy获得Map<String, List<Measurement>>,如果这是您正在寻找的:

Map<String, List<Measurement>> result = 
events.stream()
.collect(Collectors.groupingBy(Event::getDevice,
Collectors.mapping(Event::getMeasurement, Collectors.toList())));

最新更新