Julio_AWS_DevRel Julio_AWS_DevRel - 5 months ago 38
Java Question

Is there a Map implementation with listeners for Java?

I would like a Map implementation in which i could add listeners for put() events.

Is there anything like that in the standard or any 3rd party libraries?

Answer Source

I'm not aware of any standard or 3rd party, but it is easy, just create a class which wraps another Map and implements the Map interface:

public class MapListener<K, V> implements Map<K, V> {

    private final Map<K, V> delegatee;

    public MapListener(Map<K, V> delegatee) {
        this.delegatee = delegatee;
    }

    // implement all Map methods, with callbacks you need.

}