I have a large String-> Integer map, and I want to find the top 5 values โโon the map. My current approach involves translating a map into a list of an array of a pair's object (key, value), and then sorting using Collections.sort () before accepting the first 5. It is possible that the key updated its value during the operation.
I think this approach is acceptable single-threaded, but if I had multiple threads, they all started transposing and sorting often, it doesn't seem very efficient. An alternative, apparently, is to save a separate list of 5 items and update it when performing the corresponding operations on the map.
Can I offer several options / alternatives to optimize this? I am happy to consider various data structures, if any.
Thanks!
java optimization algorithm data-structures
Scruffers
source share