Home > Blockchain >  is there a collection that the item will be automatically removed in some fixed time?
is there a collection that the item will be automatically removed in some fixed time?

Time:12-20

for example ,when I add a Object to a List ,the List can automatically remove this object in some near future ?

CodePudding user response:

For your use case you should take a look at Google's Guava library:

LoadingCache<String, String> cache = CacheBuilder.newBuilder()
.expireAfterAccess(2,TimeUnit.MILLISECONDS)
.build(loader);

CodePudding user response:

It's not exactly what you are asking (since the removal is not triggered by time), but you could create a sub-class of LinkedHashMap that may automatically remove the eldest entry when a new entry is added to the Map.

This is achieved by overriding protected boolean removeEldestEntry(Map.Entry<K,V> eldest).

  • Related