Proper usage of Java Weak Reference in case of nested collections
- by Tong Wang
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.