OT: anyone aware of weak String.intern() -like collection implementations?

Problem: need a collection that will allow to implement -intern() like functionality
for immutable objects but won't hold the stored representatives by hard reference.

-


Maxim Shafirov
http://www.jetbrains.com
"Develop with pleasure!"


5 comments
Comment actions Permalink

Maxim Shafirov (JetBrains) wrote:

Problem: need a collection that will allow to implement -intern() like
functionality for immutable objects but won't hold the stored
representatives by hard reference.


I assume that what you want from this is the ability to have canonical
objects (never two copies of equal objects) while not keeping the
objects longer than necessary?

I've implemented something similar for one of my projects, where each of
those canonical objects has a unique identifier. Is this the case for
your intended usage?

In this case what I need is essentially a mapping from the identifier to
a weak reference to the canonical object, that is, a
Map>]]>, together with some additional
bookkeeping around this -- an inverted version of WeakHashMap, you could
say. My implementation is quite primitive, with only a few methods and
a plain iterator, but that could be changed without too much effort
since the real work is delegated to a HashMap.

If there is no unique identifier other than the canonical object itself,
I suppose one could have a map from weak references to weak references.

Maybe you've already thought of all of this and what you want is a high
quality publicly available implementation. Unfortunately I'm not aware
of any, but then again I haven't searched that extensively since my
needs were already satisfied.

0
Comment actions Permalink

Did you find one, or just write one yourself?
R

0
Comment actions Permalink

We took ReferenceMap from commons-collections but haven't really tested for
performance yet. So far it seem OK but there seem to be plenty of alternatives
we can use if necessary.
-


Maxim Shafirov
http://www.jetbrains.com
"Develop with pleasure!"


0

Please sign in to leave a comment.