我有这种代码和平工作正常,需要一个Map<String, List<Device>>时间并返回相同的数据结构:
Stream<Map.Entry<String, List<Device>>> results = device.getDeviceMapList().entrySet().stream();
Map<String, List<Device>> sortedMap = new HashMap<String, List<Device>>();
results.forEach(e -> {
e.getValue().sort(Comparator.comparing(Device::getStationTimeStamp));
sortedMap.put(e.getKey(), e.getValue());
});
Run Code Online (Sandbox Code Playgroud)
现在我尝试使用Collectors.toMap并没有成功:
Map<String, List<Device>> sortedMap = results.forEach(e -> {
e.getValue().stream()
.sorted(Comparator.comparing(Device::getStationTimeStamp))
.collect(Collectors.toMap(e.getKey(), ArrayList<Device>::new));
});
Run Code Online (Sandbox Code Playgroud)
这部分.collect(Collectors.toMap(e.getKey(), ArrayList<Device>::new));是我试过的,它不完全正确,我做错了什么?