I have an application without a save button; saving happens automatically in the background. As the user works with the app, tasks are created and put into a queue for execution. Some of them are delayed, for example, when you start typing, I wait 200ms before I update the corresponding value in the database.
To make this more simple to use code wise, I'd like to be able to add a task to the queue with a delay and a "key". If there is already a task with the same "key", then my second task should be ignored (because the same operation is already queued).
Does something like that already exist?
I think you should look into TreeMap class. Use the constructor which let you pass a Comparator. This way you can let the Map sort on Delayed (since this class implements Comparable interface). Before adding a "Task" to the map, check if the key already exists using containsKey method.