Sunil Kumar Naik Sunil Kumar Naik - 1 year ago 92
Java Question

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

How can I Use Collectors to collect in a ConcurrentHashMap instread of putting manually into ConcurrentHashMap

ConcurrentHashMap<String, String> configurationMap = new ConcurrentHashMap<>();
List<Result> results = result.getResults(); -> {
res.getSeries().stream().forEach(series -> {
series.getValues().stream().forEach(vals ->{

//Note: vals is List<List<Object>> type

Help will be appreciated.

Answer Source

You can use Collectors.toConcurrentMap
           .flatMap(res -> res.getSeries().stream())
           .flatMap(series -> series.getValues().stream())
                                  vals -> vals.get(1).toString(),
                                  vals -> vals.get(2).toString()));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download