Why does exist WeakHashMap, but absent WeakSet?

后端 未结 3 1506
名媛妹妹
名媛妹妹 2020-12-04 15:29

From J. Bloch

A ... source of memory leaks is listeners ... The best way to ensure that callbacks are garbage collected promptly is to store o

3条回答
  •  粉色の甜心
    2020-12-04 15:45

    While you can indeed use Collections.newSetFromMap() to get a WeakSet, it's use cases are actually quite limited.

    If you want to implement something like String.intern() you might want to have a look at Guava's Interners.newWeakInterner() functionality instead.

提交回复
热议问题