是否有固定大小的队列来删除多余的元素? [英] Is there a fixed sized queue which removes excessive elements?

查看:435
本文介绍了是否有固定大小的队列来删除多余的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个固定大小的队列。当我添加一个元素并且队列已满时,它应该自动删除最旧的元素。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆