23

How do I get a ConcurrentHashMap with weak keys and identity hashes in Java? I think Google Guava Collections can give such a thing, but can I get it from the standard library? What other options do I have?

4 Answers 4

15

I think Google Guava Collections can give such a thing, but can I get it from the standard library?

The short answer to that is No. Java SE does not implement this particular combination.

  • You could instantiate a java.util.concurrent.ConcurrentHashMap with WeakReference keys, and do some extra work to implement removal of map entries for broken references, but that won't give you identity hash semantics.

  • You could instantiate a java.util.IdentityHashMap with WeakReference keys, and do some extra work to implement removal of map entries for broken references, but that won't give you concurrent behaviour.

  • Using a java.util.WeakHashMap won't give you either concurrency or identity hashing.

  • You could (in theory) wrap the key class in something that overrode the natural equals and hashcode methods. But that is most likely to be unusable.

  • I don't think it would be possible to do this by overriding methods in either ConcurrentHashMap or IdentityHashMap.


Maybe the only viable option would be to change the key classes equals and hashcode methods to be identity based. But that won't work for "built in" key types (especially final ones) or for cases where you need value-based equals/hashcode in other parts of the application.

15
  • Any comments about possible options?
    – r.v
    Commented Mar 17, 2013 at 2:08
  • 1
    Use Google Guava. That's an option. (Any reason not to use it?) Or write your own implementation from scratch.
    – Stephen C
    Commented Mar 17, 2013 at 2:09
  • 1
    Please, for the love of people that come after you, don't write your own from scratch. Use Guava. Commented Mar 17, 2013 at 7:32
  • 3
    @Souvik new MapMaker().weakKeys().makeMap(). See the apidoc. This uses Guava and is not exactly java.util.concurrent.ConcurrentHashMap but will be a drop-in replacement.
    – r.v
    Commented Aug 31, 2013 at 0:43
  • 1
    All the work arounds in these comments are wrong. Guava doesn't compare WeakReferences by the identity of the referent value, it compares by the identity of the WeakReference, which is new every time. So it won't work. It's basically useless, truthfully. ConcurrentHashMap<WeakReference<K>, V> has the same issue. Commented Dec 8, 2020 at 17:23
5

The Google Guava implementation appears the easiest way to go. One may initialize the required map with new MapMaker().weakKeys().makeMap() and use just as one would use java.util.concurrent.ConcurrentHashMap. See the apidoc for more details.

3
  • That's correct – why was it downvoted? You should mentiont that using weak keys causes key equality to be evaluated with ==. Commented May 25, 2017 at 22:39
  • @LaurentCaillette It was probably downvoted because OP specifically asked for a solution that does not use a 3rd-party library
    – Gili
    Commented May 15, 2018 at 14:11
  • It was downvoted because evaluating key equality with == on WeakReference makes it basically useless. Commented Dec 8, 2020 at 17:28
2

if your application is under spring framework ( version is gt 3.2 ), you can consider to use org.springframework.util.ConcurrentReferenceHashMap. Below is its description:

A ConcurrentHashMap that uses soft or weak references for both keys and values. This class can be used as an alternative to Collections.synchronizedMap(new WeakHashMap>()) in order to support better performance when accessed concurrently. This implementation follows the same design constraints as ConcurrentHashMap with the exception that null values and null keys are supported.

NOTE: The use of references means that there is no guarantee that items placed into the map will be subsequently available. The garbage collector may discard references at any time, so it may appear that an unknown thread is silently removing entries.

If not explicitly specified, this implementation will use soft entry references.

2
  • The OP asks for a Map with three characteristics: 1. Concurrency 2. Weak Keys 3. Identity Hash. Spring only provides the first two with its ConcurrentReferenceHashMap.
    – jaco0646
    Commented Jun 7, 2020 at 13:20
  • 1
    ConcurrentReferenceHashMap also has weak values. It will remove an entry if the value has no hard references anywhere.
    – Frettman
    Commented Dec 2, 2020 at 14:34
2

search ConcurrentWeakIdentityHashMap, you will get many examples. I wrote an implement myself, for I think the hashCode of org/ehcache/core/internal/util/ConcurrentWeakIdentityHashMap$WeakReference is so bad.

Example of ehcache3

Example I wrote

Pull Rquest to fix the ehcache3 ConcurrentWeakIdentityHashMap Key hashCode

1
  • Thanks for the second link. It solves many of my probles. Works like a charm in my AI project. Had to change class scope and constructors to public though Commented Apr 26, 2017 at 22:52

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