Java 8转换列表<;地图<;字符串,字符串>;映射<;字符串,Map<;字符串,字符串>&燃气轮机;
我想转换地图列表,即Java 8转换列表<;地图<;字符串,字符串>;映射<;字符串,Map<;字符串,字符串>&燃气轮机;,java,java-stream,Java,Java Stream,我想转换地图列表,即 List<Map<String,String> Map<String,Map<String,String>>; List正如其他人所指出的,您没有为数据使用理想的结构。但在这种情况下,这应该是你想要的: Map<String, Map<String, String>> distancesBySource = distances.stream() .collect(Collectors.g
List<Map<String,String>
Map<String,Map<String,String>>;
List正如其他人所指出的,您没有为数据使用理想的结构。但在这种情况下,这应该是你想要的:
Map<String, Map<String, String>> distancesBySource = distances.stream()
.collect(Collectors.groupingBy(m -> m.get("Source"),
Collectors.toMap(m -> m.get("Destination"), m -> m.get("distance"))));
Map distancesBySource=distance.stream()
.collect(收集器.groupingBy(m->m.get(“源”),
toMap(m->m.get(“目的地”),m->m.get(“距离”);
你试过了吗?为什么不使用合适的物体?地图列表总是一种代码味道。请注意,如果您有重复的数据点,它将抛出IllegalStateException。我将您的答案投了赞成票。我的也收到了投票,所以我不会删除它。对于这种情况有“最佳实践”吗?@Fildor我认为可以(或至少可以)发布扩展评论作为答案。就我个人而言,我不认为你在Kayaman最初的评论上添加了太多内容。但看起来我在这方面是少数派。
Map<String, Map<String, String>> distancesBySource = distances.stream()
.collect(Collectors.groupingBy(m -> m.get("Source"),
Collectors.toMap(m -> m.get("Destination"), m -> m.get("distance"))));