Proper usage of Java Weak Reference in case of nested collections

Posted by Tong Wang on Stack Overflow See other posts from Stack Overflow or by Tong Wang
Published on 2010-05-08T06:44:54Z Indexed on 2010/05/08 6:48 UTC
Read the original article Hit count: 195

Filed under:

I need to define a weak reference Map, whose value is a Set. I use Google collections' MapMaker, like this:

Map<Class<? extends Object>, Set<Foo>> map = new MapMaker().weakKeys().weakValues().makeMap();

So, for Set<Foo>, can I use a normal HashSet? Or, do I have to create a weak HashSet, like this:

Collections.newSetFromMap(new WeakHashMap<Foo, Boolean>());

And why?

Another question, the key of my map is Class objects, when will a Class object become weakly reachable? In other words, what is the lifetime of a Class object?

Thanks.

© Stack Overflow or respective owner

Related posts about java