views:

81

answers:

3

Hi!

From J. Bloch

A ... source of memory leaks is listeners ... The best way to ensure that callbacks are garbage collected promptly is to store only weak references to them, for instance, by storing them only as keys in a WeakHashMap.

So, why there isn't any WeakSet in java collection framework?

Thanks.

+4  A: 
Set<Object> weakHashSet = Collections.newSetFromMap(
        new WeakHashMap<Object, Boolean>());

from javadoc in java.util.Collections#newSetFromMap(Map)

mart
actually any Set in java collection contains Map for storing.
mart
Yep, but why there is no specific class for such stuff?
Stas
It's easy to imagine why the maintainers of java.util might have wanted to stop having to provide dual Map and Set versions of everything they do, and opted to just provide newSetFromMap() instead... isn't it?
Kevin Bourrillion
@Kevin Bourrillion : It is. But there is some strange about their selections.
Stas
+2  A: 

It's simple: there are use cases for WeakHashMap (in particular, the case where you want to annotate objects with additional properties), but there are no use cases for WeakSets.

Martin v. Löwis
@Martin v. Löwis : Observer's implementation is a use case for WeakSets, isn't it?
Stas
That's debatable, and an API design decision. java.util.Observable has opted to hold strong reference to the observers. It is then the observer's choice to pass a (wrapper to) a weak reference, allowing the observable to hold the only reference to the observer - which would not possible if they were weakly referenced by default.
Martin v. Löwis
@Martin v. Löwis : Hm.. I see. It's debatable. But there are _some_ implementations where observer pattern uses WeakSets. And J Bloch know about it. if so, why there is no any special class for this? But I see.. Probably this usages is not often or smt like this.
Stas
Rationale aside, it's a matter of fact that it does, see http://google.com/codesearch/p?hl=de#-WpwJU0UKqQ/src/share/classes/java/util/Observable.java. In Java, you often create temporary objects as observers (using anonymous classes often) that wrap the notification API to the target object. These observers will only be referenced by the observable, but they will adapt the callback to perform the real action on some long-lived object.
Martin v. Löwis
@Stas: It's also really trivial to create a weak set in a single line, so what advantage would you gain if there was a separate class for it? In Python, we have the saying "not every two-line function needs to be in the standard library". The same reasoning might apply here.
Martin v. Löwis
@Martin v. Löwis : Thanks for comments.
Stas
+5  A: 

So, why there isn't any WeakSet in java collection framework?

While there may be limited use-cases for WeakHashSet, part of the Java class library design philosophy was to avoid populating the class libraries with utility classes for all possible use-cases.

There are a number of other class libraries which include collection types; Apache Commons Collections and Google Collections (aka Guava) are good examples. However, WeakHashSet hasn't even "made the cut" for the Apache and Google libraries.

Stephen C
@Stephen C : Thanks. Probably i need do more observing work about other libraries to understand java collection framework.
Stas