0

I need to create a concurrent map with <String, AtomicLong> parameters. The map should be sorted based on it's value, when I increment the AtomicLong, it should also balance itself.

The problem with ConcurrentSkipListMap is that it cannot sort the map based on its value. TreeMap is not a concurrent data structure.

I'm looking for a data structure so that complexity of insertion should be O(log(n)) like TreeMap and ConcurrentSkipListMap. Also, the values are not unique so that there may be multiple map entries that have same numeric value in AtomicLong.

burak emre
  • 1,501
  • 4
  • 22
  • 46
  • This sounds complicated. What are you trying to achieve with this? – Absurd-Mind Jul 15 '14 at 11:26
  • @Absurd-Mind, i'm working for an analytics project so it's basically a counter map. Let's say the key is an url and the values are the counts of visits. I looked at the libraries like Guava but couldn't find and appropriate data structure for this case. :/ – burak emre Jul 15 '14 at 11:29
  • Did you consider using `synchronized` for access on the map and the values and instead use a simple `TreeMap` and `Integer`? – Absurd-Mind Jul 15 '14 at 11:31
  • Yes but the problem is TreeMap is also keeps the list sorted based on key and even though I use some hacks to sort it based on value, when I increment the counter, it cannot recognise the change and balance the map again. – burak emre Jul 15 '14 at 11:34
  • Did you try using two Collections? `HashSet` and `PriorityQueue`, where `class UrlCounter{ String url; int count = 0; }`? (or TreeSet). I still would suggest that you describe your complete problem a bit better, so that more appropriate solutions can be given. Also see this: http://mywiki.wooledge.org/XyProblem – Absurd-Mind Jul 15 '14 at 11:59
  • I have been working on a distributed analytics service and each node in the cluster is responsible for a set of counters. Also each node has multiple threads that can access the internal UrlCounter objects. When a user views an url, the system automatically routes the request to the node that is responsible for that key. If the key (url string) exists in internal map, i need to increment the counter in UrlCounter, otherwise i need create a new AtomicLong(1) and put it to the map. The UrlCounter instances in map must be sorted and we should be able to get the top N url without any latency. – burak emre Jul 15 '14 at 13:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57330/discussion-between-burak-emre-and-absurd-mind). – burak emre Jul 15 '14 at 13:20

0 Answers0