This week's book giveaway is in the General Computing forum.
We're giving away four copies of Emmy in the Key of Code and have Aimee Lucido on-line!
See this thread for details.
Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Urgent: "coLLection timeout"

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,
I want a collection which automatically deletes entries older than some specified amount of time. I was considering LinkedHashMap which has the provision to delete eldest entries. But, how can I program it to automatically delete an entry for which some specified amount of time has elapsed since being put in the collection. One thought is to make the entry implement some kind of a

time listener

. Please tell me how to do this. It is very urgent.
thanks in advance
raamam
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Off the top of my head, why not use a stack - every time an entry is used, remove it from the current position and add back to the top of the stack. That way the least recently used entries will gravitate to the bottom of the stack. Your entry will of course have to include some sort of timestamp - alternately you could just let the stack never grow beyond a certain size, discarding elements from the bottom to make room.
Bill
 
lowercase baba
Posts: 12772
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm not sure a stack is right... at least, in the traditional sense. all you can do with a stack is pop off the top element. how do you get an element that's 8 or 15 or 97 down?
of course, maybe i'm being to literal in my thinking of a stack, but i'm not sure how to implement something like this...
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all you can do with a stack is pop off the top element. how do you get an element that's 8 or 15 or 97 down?
I'd say LinkedHashMap is the way to go here. As Bill says, you need a timestamp of some sort for each entry. The simplest way would be for each key, store a Date object representing the time at which the entry was put in the Map - or the time of last access, if that's what you prefer. Depending what you mean by "older". And make sure the LinkedHashMap is in the appropriate mode for whichever "older" definition you're using. (See the constructor that takes a boolean accessOrder argument.)
Anyway, to keep deleting entries once they reach a specified age, I'd create a separate thread for the task. The thread should run a continuous loop which locates the oldest entry in the Map and checks the Date to see if it is old enough to be deleted. If it is, then delete that entry and check the next-oldest. If it's not old enough, find the difference in milliseconds between the current time and the time at which the entry should be deleted, and do a Thread.sleep() for that time. When the thread wakes up it should go back to the start of the loop, so that it again locates the oldes element in the Map and checks its Date. It's possible that the entry that was previously looked at has been deleted since then, so there may be nothing to delete now. Make sure to use synchronization since you're accessing the LinkedHashMap from at least two threads.
[ December 02, 2003: Message edited by: Jim Yingst ]
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

'm not sure a stack is right... at least, in the traditional sense. all you can do with a stack is pop off the top element. how do you get an element that's 8 or 15 or 97 down?


Since java.util.Stack extends Vector you can use the Vector methods such as removeElement( obj ) and remove( n ).
I still like the Stack idea as long as the number is not too huge - the contains( obj ) method has to do a linear search so a huge number would be slower than some sort of faster collection like LinkedHashMap.
Bill
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since java.util.Stack extends Vector you can use the Vector methods such as removeElement( obj ) and remove( n ).
Yeah, but that's because a Stack is not a stack, in the traditional sense, as Fred said. But I'd agree that a Stack would work fine as long as the size is not substantial. Also, I think you're assuming that "oldest" meanst "least-recently-accessed". If it means "first added" then the stack won't work as well. But the assumption is probably correct (for applications I can think of offhand, anyway) so indeed, the idea is worth considering.
 
We don't have time for this. We've gotta save the moon! Or check this out:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!