Hi is here any way how i could attach an event to a hashmap. For example I want a method called "Somemethod" to get trriggered when i put smthing in the container . Any suggestions on how i could accomplish tht ??
Jeanne Boyarsky wrote:You could put the map in a class so you have to call that class to update the map and have that helper/wrapper method call the method you want called. Not sure if there is a better way.
Yeah thats what i had in mind. cusrious if there was some other way
In addition to what Paul said, I would suggest adding a java.beans.PropertyChangeSupport member in the subclassed HashMap, and methods for add/removePropertyChangeListener(...) [ edit: and getPropertyChangeListeners(...) ] that forward to the PropertyChangeSupport. Then, in methods of interest you could override to call the super implementation (i.e. HashMap implementation) and (maybe conditionally) fire a propertyChange. That would notify all registered PropertyChangeListeners.
I'm not sure if PropertyChangeListener is the right choice. That requires the use of property names (Strings), where this solution would need type K (or the more general Object) for the key. Perhaps a custom listener / event pair is better:
I once created such a framework (listeners, events, wrapper classes around Maps, Collections, Lists, Sets, etc) but deleted it because I never used it.