commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jeff Keyser" <>
Subject RE: [Collections][SUBMIT] WeakHashSet
Date Sun, 28 Apr 2002 23:54:11 GMT
Comments in-line below...

> -----Original Message-----
> From: Jack, Paul []
> Sent: Friday, April 26, 2002 6:34 PM
> To: 'Jakarta Commons Developers List'
> Subject: RE: [Collections][SUBMIT] WeakHashSet
> Hi,
> I have a few thoughts regarding your WeakHashSet...
> My first thought was to somehow integrate it with the
> whole Transformer/Predicate collections we've been
> discussing in another thread.  So you'd have a Transformer
> that converted an object into a WeakReference before
> storing it in the set.  However, you'd need *another*
> Transformer to convert those WeakReferences back into
> their referents when taking the objects out of the set.
> From what I understand, the proposed Transformer collections
> only transform when objects are added.  Furthermore it's
> unclear whether a two-Transformer based implementation would
> work in all the algorithms for weak references (you'd still
> need to empty the ReferenceQueue on a regular basis...)
> My second thought is, the two Set implementations in the JDK
> are backed by Map implementations.  WeakHashSet could be
> implemented as a Set that's backed by a WeakHashMap.  I'm
> thinking of having a static wrapper method in CollectionUtils
> that would provide a Set backed by *any* map:
>   // Creates a Set backed by the given map.  Given
>   // map must be empty.
>   public static Set createSet(Map map);
>   public static SortedSet createSortedSet(SortedMap map);
> This way, a user could get a weak hash set by calling
> CollectionUtils.createSet(new java.util.WeakHashMap()).
> Or, they could use soft references instead of weak
> references in their set by calling
> CollectionUtils.createSet(new
> org.apache.commons.collections.SoftRefHashMap()).
> This also limits the number of classes we'd have to develop
> to provide the Predicate and Transformer collections...

This kind of depends on how a Set is backed by a Map.  (You'll have to
forgive me for not knowing how this works.)  For example, in a WeakHashMap,
it is the keys that are held with weak references.  If while backing the Set
with this Map, the Set's values are stored in the Map's values and not its
keys, then it may not work as expected.  If it works as expected, I have no
objection to this.

> What do you think?

I hadn't really been following the Transformer/Predicate conversation, but
from what I could gather from your comments above and from your last few
messages on that topic in this list, I really started thinking more about my
approach to this.

Basically, it is the "weak reference" part of what I've done that's really
important (and the part I originally needed), not the fact that it's stored
by hash.  If someone wanted a List with weak references, that would have to
be written again.

So maybe what I really need to do is write a "weak reference Set wrapper,"
and encapsulate whatever Set the user passes.  Basically, doing something
similar to your Transformers and Predicates, that wrap underlying
collections.  Perhaps the natural extension is to go to Lists, etc. as you
are discussing with Transformers and Predicates.

Which brings me to another thought - Ultimately, this "weak reference
Collection wrapper," Transformers and Predicates are all collection wrappers
of one sort or another that encapsulate another collection that actually
stores the objects (or another wrapper).  Perhaps they should all subclass
from the same class, or implement a common interface?

> -Paul

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message