Adding concurrency with iterating over a collection, mapping to multiple hash maps and reducing to one

纵然是瞬间 提交于 2019-12-13 10:51:35

问题


Have a specific use case and not too sure of the best approach.

So the current approach right now is that I'm iterating over a collection of objects (closeable iterator) and mapping them in a hashmap (dealing with conflicts appropriately, comparing by an object's date property).

I'm looking for a parallel approach to speed things up and the initial idea was to use Java 8 streams with parallel and forEach utilizing a concurrent hashmap to enable concurrency. The main bottleneck with this approach seems to be the concurrent hashmap and there are no improvements with this approach at adding concurrency.

My idea right now is to map into multiple hashmaps in parallel. Then reduce into a single hashmap dealing with conflicts appropriately. Wondering how I would implement such an approach in Java using Streams. I know there are built in map and reduce operations but not sure about the approach of collecting multiple hashmaps (perform the operation and add to a set?).


回答1:


Try using the groupingBy() function. For example

stream.collect(Collectors.groupingBy(Locale::getCountry));

generates a map that has the countries as keys and the Locales with that country as values



来源:https://stackoverflow.com/questions/51330711/adding-concurrency-with-iterating-over-a-collection-mapping-to-multiple-hash-ma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!