I have an Array of Map. In which I need to do some calculation and return Another Map with the addition of the same key count into the value in the result. I have tried the below one. But as it's running parallel, it does not add it.
Please help to improve it
I can able to achieve this in a normal for loop.
public Map<Long,Long> solve(Map<String,Stats>... map){
Map<Long,Long> resultCount = new HashMap<Long,Long>();
if(map != null){
resultCount = Arrays.stream(map).filter(Objects::nonNull).map(map -> getUserCountMap(map))
.collect(HashMap::new, Map::putAll, Map::putAll);
}
return resultCount;
}
public Map<Long,Long> getUserCountMap(Map<String, Stats> map) {
Map<Long,Long> resultCount = new HashMap<Long,Long>();
map.forEach((k,v)->{
try {
String key = (String) k;
Stats userValue = (Stats) v;
Long userId = new Long(key);
System.out.println("key :::"+key+":::"+resultCount.getOrDefault(userId, 0l));
Optional<Long> count = userValue.getCount();
System.out.println(count.get());
count.ifPresent(aLong -> resultCount.put(userId, (resultCount.getOrDefault(userId, 0l) + aLong)));
System.out.println(resultCount);
} catch (Exception e) {
}
}
);
System.out.println("ret "+resultCount);
return resultCount;
}
Any good document to understand Java 8 Streams API and various intermediate and terminal operations