问题
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