Does that mean that a value should never refer to its or any other key at all in the
WeakHashMap
so not to prevent garbage collection?
A value shouldn't refer to its own key, but it can refer to other keys.
And how exactly is a strong reference by a value to its key made?
If the keys are String
s, and the values have a String
data field, you can set that String
field to the object's key to create a strong reference from the value to its key.
As an example, I need to hold a list of IP and DNS addresses associated with the key, a connection. How can I ensure that those are not strong references to the key's
InetSocketAddress
thenInetAddress
?
An InetAddress
doesn't have a strong reference to the connection you got it from. There is no constructor that takes a WebSocket
, so there is no way an InetAddress
will have a reference to the WebSocket
you got it from.