具有最大大小且无重复的先进先出集合?
.NET 似乎有很多数据结构和集合类型。它是否有一个具有最大大小且没有重复的先进先出集合,或者类似的东西?
一个示例用法是存储 5 个最近打开的文件。如果添加了第 6 个对象,则将最近的对象出列以将大小保持为 5
.NET seems to have a lot of data structure and collection types. Does it have a first-in-first-out collection with a maximum size and no duplication, or something similar to that?
An example usage would be like for storing 5 most recently opened files. If a 6th object is added, dequeue the least recent object to keep the size to 5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须创建一个实现
ICollection
的QueueSet
。它可以是包含集合作为后备存储的包装类。它可以按如下方式实现:我将此代码发布到公共领域。
You'll have to create a
QueueSet
that implementsICollection<T>
. It can be a wrapper class that contains a collection as a backing store. It can be implemented as follows:I release this code into the public domain.
您想要一个队列,不是吗?
它并不真正支持最大大小和无重复,但您可以从 Queue 继承并添加这些功能。
您可以通过重写 Enqueue 方法来做到这一点。像这样的东西可能会起作用(但抛出更合适的异常类型!):
包含类或包装类或 HAS-A 类可能如下所示:
You want a queue, no?
It doesn't really support maximum size and no duplication but you could inherit from Queue and add those features.
You could do so by overriding the Enqueue method. Something like this might work (but throw more appropriate exception types!):
A containing or wrapper or HAS-A class might look like this:
看看这个: 限制队列大小在.NET中?
你基本上需要一个
Queue
,如果你设法限制它的大小并让它“索引”或“唯一”,那么你就可以了:)我相信一点逻辑围绕
Dictionary
也可以,您将存储的数据类型是什么?字符串?have a look at this: Limit size of Queue<T> in .NET?
you basically need a
Queue
, if you manage to limit its size and get it "indexed" or "unique" then you are ok :)I believe a bit of logic around a
Dictionary
would also work, what is the data type you will store? Strings?这就是您想要的,
HashQueue
,哈希队列集。添加了一些线程锁,防止意外锁定。请记住,所有 HashSet 操作都会破坏现有队列的顺序。
This is what you want,
HashQueue<T>
, the hashed queued set.Added some thread locks, protects from accidental locks. Keep in mind, that all HashSet operation breaks the order of existing queue.