是否有固定大小的队列可以删除过多的元素? [英] Is there a fixed sized queue which removes excessive elements?
问题描述
我需要一个固定大小的队列.当我添加一个元素并且队列已满时,它应该自动删除最旧的元素.
I need a queue with a fixed size. When I add an element and the queue is full, it should automatically remove the oldest element.
Java 中是否有针对此的现有实现?
Is there an existing implementation for this in Java?
推荐答案
Java 语言和运行时中没有现有的实现.所有队列都扩展了 AbstractQueue,并且它的文档清楚地声明将元素添加到完整队列总是以异常结束.最好(并且非常简单)将 Queue 包装到您自己的类中以获得您需要的功能.
There is no existing implementation in the Java Language and Runtime. All Queues extend AbstractQueue, and its doc clearly states that adding an element to a full queue always ends with an exception. It would be best ( and quite simple ) to wrap a Queue into a class of your own for having the functionality you need.
再说一次,因为所有队列都是 AbstractQueue 的子级,只需将其用作您的内部数据类型,您就应该有一个几乎立即运行的灵活实现:-)
Once again, because all queues are children of AbstractQueue, simply use that as your internal data type and you should have a flexible implementation running in virtually no time :-)
更新:
如下所述,有两个可用的开放实现(这个答案已经很老了,伙计们!),请参阅这个答案 了解详情.
As outlined below, there are two open implementations available (this answer is quite old, folks!), see this answer for details.
这篇关于是否有固定大小的队列可以删除过多的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!