A limited-sized dictionary that removes the oldest elements?

Is there an existing data structure used for hash data that will make it possible to delete the oldest element?

The approach I'm thinking about now is that the dictionary and the queue have a quick search using the Dictionary and the ability to delete the oldest element from the Dictionary using the queue.

+8
c # data-structures
source share
3 answers

You can use OrderedDictionary . This will maintain the insertion order (as opposed to SortedDictionary , which will be ordered by keys). Then you can remove the first available item that would be considered the oldest.

+10
source share

System.Collections.Generic.SortedList is just a dictionary that sorts by key. If the key is temporarily somehow, you can simply use RemoveAt to remove the first element when you reach a certain size, when you want to add another record.

Probably since you mentioned Dictionary , you probably don't have a temporary key. But, a Dictionary is just a collection of KeyValuePair<K,V> objects. Thus, you can have a sorted list where the value is KeyValuePair<K,V> , and the key is the date / time the item was added.

0
source share

Since you have a specific requirement, I would have a Dictionary with a queue / buffer (for example, the circular buffer mentioned in the comments).

OrderedDictionary is a good choice and a good source of how to do this (I don't want to post it here, but you can easily find it) - you just need something better than an ArrayList to hold your elements (and do dequeueing) - since you are constantly deleting the "first".

0
source share

All Articles