如何使用收集器以手动方式将其放入ConcurrentHashMap中的ConcurrentHashMap中进行收集
ConcurrentHashMap<String, String> configurationMap = new ConcurrentHashMap<>(); List<Result> results = result.getResults(); results.stream().forEach(res -> { res.getSeries().stream().forEach(series -> { series.getValues().stream().forEach(vals ->{ configurationMap.put(vals.get(1).toString(),vals.get(2).toString()); }); }); }); //Note: vals is List<List<Object>> type
帮助将不胜感激。
我们还可以按照以下步骤进行操作:
results.stream() .flatMap(res -> res.getSeries().stream()) .flatMap(series -> series.getValues().stream()) .collect(Collectors.toMap( vals -> vals.get(1).toString(), vals -> vals.get(2).toString(), (vals1,vals2) -> vals2, ConcurrentHashMap::new);