• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Attach an event to a hashmap

 
Rajesh Khan
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ??
 
Paul Clapham
Sheriff
Pie
Posts: 20980
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way is to write a subclass of HashMap which overrides the add() method -- or is it called "put", I can't remember offhand -- to do that.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34210
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rajesh Khan
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont think there's binding or trigger like feature in java, so the only way i can think of is already mentioned by Jeanne and Paul.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

If the notification has to go out to any objects of Swing classes, you might want to use a javax.swing.event.SwingPropertyChangeSupport, which invokes the listeners' propertyChange(...) on the EDT.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic