Is there a "decaying map" in Java?

Thinking it might be okay to just use timers or something for now.

@carlozancanaro You put an element into it, after a certain amount of time it will be removed, unless fetched, updated or "poked".

@gudenau I don't know of anything time-based, but java.lang.ref.SoftReference lets you to build data structures that will hold objects until there's memory pressure. I think Guava has a SoftHashMap implementation.

What's the use case for you?

@shadowfacts @carlozancanaro I suppose so, the main thing I would need to use it for would be to make things purge themselves after a delay. So it would require a specific interface and callbacks for when the objects are purged. Plus some sort of iteration thing for expired tasks on the ticks.

@gudenau @shadowfacts It looks like Guava has something similar to what you want.

You can add a removal listener to the cache, which isn't mentioned in that answer but is in the javadoc.

bad fiddler on the roof joke 

bad fiddler on the roof joke 

@gudenau @carlozancanaro Callback is easy, just require that V extend some interface w/ a method that's called on expiry.

You could also keep a Set<V> expired; that's cleared every time step.

To purge expired objects automatically, you'd just have a method that iterates all objects and expires them if necessary, called every time step from a run loop or a worker thread.

@shadowfacts @carlozancanaro Yeah, I was thinking about this wrong. A LinkedList backed thing would probably be best for this, since it would be quick to iterate and once you run out of expired things you know you can stop.

@gudenau @carlozancanaro If you don't need to look things up by keys, yeah that'd work well. Just need to insert new and refreshed objects at the head.

@shadowfacts @gudenau If it's doubly-linked then I don't think it matters which end you insert.

If it's singly linked then you should insert at the tail so you can invalidate items by starting at the head and going forwards until you hit a valid one.

Although if you don't need to do any special processing to invalidate individual entries then you should insert at the head and just drop the entire tail when you read the first invalid item.

@carlozancanaro @shadowfacts You would still need to iterate over all the dropped items to notify them that they where dropped.

@carlozancanaro @shadowfacts Would be a per-use case thing if you did not need to do processing. Might be an interesting paper.

@gudenau @shadowfacts I don't think this is paper worthy. This is pretty basic data structure stuff.

@carlozancanaro @shadowfacts I meant the "drop the head or drop the tale", not a long one mind.

@shadowfacts @gudenau That implementation will only remove elements once the user has attempted to "get" them. For some use cases this map will leak memory because it will fail to expire keys that the user no longer cares about.

Sign in to participate in the conversation
Mastodon for Tech Folks

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!