1

I'm reaching back to a previous question I had: Frequently Used metadata Hashmap

However, this time how would I adapt that solution to work with ConcurrentMap? LinkedHashMap isn't a synchronized collection, and due to performance reasons I don't want to wrap it in sycronizedMap. Is there an alternative [concurrent friendly] solution to solve this problem?

The original problem was that I need a hash map that gets rid of the least used items after so many entries.

6
  • 3
    Do you actually get performance problems if you wrap it in a synchronizedMap? Or is this a case of premature optimisation?
    – user545680
    Commented Nov 18, 2011 at 7:02
  • 1
    You may want to check out: code.google.com/p/concurrentlinkedhashmap/wiki/Design
    – Deco
    Commented Nov 18, 2011 at 7:06
  • Why don't you use ConcurrentHashMap from java.util.concurrent?
    – pushy
    Commented Nov 18, 2011 at 7:42
  • I haven't had to test out the performance of sychMap ... theres a bench mark in Currencency in Practice by Gorltz/Bloche, et al. The design of concurrentmap is rather different.
    – monksy
    Commented Nov 18, 2011 at 7:46
  • 1
    ConcurrentHashMap is particularly useful is you have multiple threads and all they do is access one Map. If you have multiple threads which spend most of their time doing something else. i.e. a relatively low percentage of the time there is two threads trying to access the same map, then it doesn't matter which one you use. Commented Nov 18, 2011 at 7:58

1 Answer 1

1

The Guava MapMaker and CacheBuilder classes can generate concurrent maps with an LRU eviction policy.

The CacheBuilder class is preferred for caching use-cases, as the Guava developers are in the process of stripping the cache support methods out of MapMaker.

2
  • 2
    CacheBuilder is our replacement for MapMaker and should be preferred.
    – Ben Manes
    Commented Nov 18, 2011 at 7:28
  • I'll accept this answer only because it leads to CacheBuilder.
    – monksy
    Commented Nov 19, 2011 at 21:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.