Aging Objects in a Collection [closed]
-
08-07-2021 - |
Pergunta
I want to be able to age objects in a Collection.
For example I could put strings in a HashSet.
- I would like timestamps associated of when the String was put in the HashSet.
- I would like String older than say time X to be dropped from the HashSet.
- I would like to be able to say "touch" a string and update the time to when it was inserted in the Collection.
So I can accomplish this by keeping track of each String and then running a house cleaning Thread and purging String from the Collection if it is older than a certain time quantum.
However I am looking for:
- Either a Clever way to do this.
- An existing open-source java component which accomplishes this.
Thanks.
Solução
Sounds like you need a cache. Either try CacheBuilder
(see Caches Explained) from Guava or EhCache.
Outras dicas
Google Guava caches meet all your requirements and more! See Caches Explained.
I would use JCS, really like the Guava idea though @sjr
Why not just create your own Collection class (for the house cleaning tasks) that's backed by a HashMap<String, Date>
?
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow