开发者

System.Collections best choice for my scenario

I want a collection for storing two types: string and DateTime.

The string should be the key of my collection and the DateTime is the insertion time of it into the collection. I want to remove items from the collection in a FIFO manner.

The collection should reject duplicate keys and queryable by DateTime so if want to now the number of items older than a given date it开发者_开发问答 could answer.


There is no single builtin C# datatype that do all those things with maximal efficiency, mostly as you indicated two things you'd have to lookup by.

That being said, a Dictionary<string, DateTime> will be the simplest solution that gives you all the features you need, basically out of the box. However, that collection will give O(n) complexity for the DateTime lookups, and worse-than-O(1) removal time. That is probably not a big deal, but you didn't describe your performance requirements, the expected sizes of your dataset, or which access types happen most frequently.

To improve on the "older-than-DateTime" lookup performance and the FIFO removal, you could also keep a second index, such as a SortedList. More memory usage and somewhat-slower overall insertion time but DateTime and removal queries will be faster. For "older-than-DateTime" you can use a binary search of the SortedList.Keys.


It sounds like System.Collections.Generic.Dictionary<string, DateTime> should do your trick. It has methods to process the collection as you need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜