user1769946
user1769946

Reputation: 31

java: updating a hashmap automatically

so right now i have a hashmap which stores Calendar objects (just need a way to store dates). each value stores two Calendar objects, one representing the start of an event, and the other the end of it. The user enters these through a gui, and they can span weeks and months.

What i need to do is be able to remove these entries when they expire, meaning their second Calendar object has passed according to the real world date. I want this to be done automatically as long the instance is running.

My idea was to have a timer thread running and once a week it could go through all the entries in the hashmap and remove the ones that have expired. I know its not all that practical, its for a school project. But I was just looking for ideas or design pattern that could help implement this in an efficient manner.

Thanks and let me know if you need any more information.

Upvotes: 3

Views: 1590

Answers (5)

Jordi Laforge
Jordi Laforge

Reputation: 670

Maybe this is useful for: http://code.google.com/p/guava-libraries/wiki/CachesExplained Guava has a "caching"-mechanism, that handels timeouts, ...

So the handling is made for you, but take care: There is no thread running in the background. The maintaince is done during the wirtes. Infos are contained in the link too.

The guava-solution "explains" the other way. Why not handling a list/map of timeout-elements which tell you during the access, that they timed out, or shutdown on their own. You don't have to manage the elements from the outside, they could handle the timeout on their own. This way is ofter quite simple and transparent to the user.

Upvotes: 1

Adeel Ansari
Adeel Ansari

Reputation: 39907

The other option can be to instantiate a TimerTask for every entry. Implement its run() method to remove the entry from the map, at schedule() time -- which is probably end time in your case. That way, you don't need weekly clean-up and the entries will be removed as soon as they expires.

And of course, take care of thread safety, perhaps by using some thread-safe variant, I mean Map implementations.

[Edited]

Or still better, use ScheduledThreadPoolExecutor, instead.

Upvotes: 0

Bohemian
Bohemian

Reputation: 425208

Using Calendar will only lead to grief - it's a terrible class, loaded with pitfalls and bugs.

Instead, store the start and end as long values, as per Date.getTime() and compare these with System.currentTimeMillis(), to keep things clean and simple.

Upvotes: 1

Manos
Manos

Reputation: 151

You can create instead a TreeSet

TreeSet<DateObject> yourTreeSet = new TreeSet<DateObject>(new DateComparator());

Then implement your comparator, and the objects will be sorted as you add them in the TreeSet

Upvotes: 0

Darren
Darren

Reputation: 792

Having a background thread that performs maintenance tasks is a good idea and is very common practice. Be warned though that a Hashmap is not thread safe so you will need to synchronize its access, or replace with a thread safe class such as ConcurrentHashMap.

Upvotes: 1

Related Questions