my dog learned polymorphism
The moose likes Java in General and the fly likes Map object with some special features. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Map object with some special features." Watch "Map object with some special features." New topic

Map object with some special features.

Madhiarasan Ramakrishnan

Joined: Jan 12, 2010
Posts: 1
I need a collection map (user defined map) which should automatically removes the old objects which are older than 5 secs ( which should be specified dynamically).

I have a MDB to read a message from the queue and put it into hashmap, there is a another client program which constantly looking for this hashmap for the particular key.

sometime the client program may get timed out before it reads the key from hashmap,
in that situation the key entry should be deleted automatically by the user defined object after some point of time.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

Madhiarasan Ramakrishnan wrote:a MDB

A what? A little Googling helped me found out you meant a Message Driven Bean, but my first guess question when I read this thread was "what does an MS Access database have to do with reading messages from a queue?". Next time, please don't use acronyms that aren't 100% clear to everybody.

As for the problem, have you searched around? Have you thought of a way to solve this yourself? I think I've seen an implementation for this once but I can't remember where.

How To Ask Questions How To Answer Questions
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

and Welcome to Javaranch
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 160

My solution might not be the best, but I think I'd do it by extending HashMap, and using a helper class that contains a value and a timeAdded field as values in the extended HashMap class. I'd override the put() method to add instances of the helper class, and override the get() method to return the value of the helper class mapped to a particular key. That way, it's completely abstracted to the developer. Lastly, you could just have a timer which periodically loops through all of the values in your maps and remove those which were added more than 5 seconds ago. (Hint: Use System.currentTimeMillis())

"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

Well that's one way. I wouldn't use this map directly though; it will have issues with values() and entrySet() as these will contain the helper classes. You could use that map inside a wrapper similar to those of Collections.synchronizedMap and Collections.unmodifiableMap.

Another issue is when to remove entries. You can use a timer like you suggested. Another option is to remove on demand. The get method would look a bit like this then:
Of course entries may need to be invalidated when using several other methods as well. Think of methods like containsKey and containsValue, and the iterators over the keySet(), values() and entrySet() collections.

So the timer is probably the easiest solution if you can at least keep synchronization issues at bay.
I agree. Here's the link:
subject: Map object with some special features.
It's not a secret anymore!