Skip to content

Remove keys when values are nullified #2

@jimka2001

Description

@jimka2001

I don't really understand the Java implementation of WeakValueHashMap. I'm using this in a Scala project. The behavior in Scala is that when values become otherwise unreferenced, then the hash map value is nullified, i.e., the object is removed from he value position and is replaced by null. For my application it would be much better if the key/value pair were simply removed from the hash map.

Can I achieve this with this class? I'd be happy if it worked this way automatically, or if there were a Boolean flag on the constructor to specify this behavior, or even if I could register a callback which would be called with the hash map and key as argument whenever a value has been (or is about to be) nullified. That would give me a chance to remove the key myself.

Otherwise, it is not really feasible (in terms of runtime complexity) to search the hash table periodically for null values, and remove the corresponding keys.

I'd appreciate either advise, enhancement, or a clue about how to enhance the code. I'm happy to create a PR, but I'm not a java-ist. I've never written a line of java in my life.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions