小编Rob*_*per的帖子

Map <String,List <Object >>的Collectors.toMap无法解析

我有这种代码和平工作正常,需要一个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));是我试过的,它不完全正确,我做错了什么?

java java-8 java-stream

4
推荐指数
1
解决办法
379
查看次数

标签 统计

java ×1

java-8 ×1

java-stream ×1