How can I use Collectors instead of manually entering ConcurrentHashMap in java 8

How can I use collectors for collection in ConcurrentHashMap to insert manually in 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 

Help will be appreciated.

+6
source share
2 answers

We can do this as follows:

 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); 
+2
source

You can use Collectors.toConcurrentMap

 results.stream() .flatMap(res -> res.getSeries().stream()) .flatMap(series -> series.getValues().stream()) .collect(Collectors.toConcurrentMap( vals -> vals.get(1).toString(), vals -> vals.get(2).toString())); 
+4
source

Source: https://habr.com/ru/post/1011796/


All Articles