java 如何从java中的队列中删除特定元素(不是优先级队列)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45798281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:54:36  来源:igfitidea点击:

how to remove a specific element from queue in java(not priority queue)

javaqueue

提问by Ranjeet Kumar yadav

How I can remove a specific element from queuein java(not priority queue).There is no functionality of removing queue.remove(object). Please help me in this.

如何从queuein 中删除特定元素java(not priority queue)。没有删除queue.remove(object). 请帮助我。

Queue<String> queue = new LinkedList<>();
queue.add("hello");
queue.add("world");
queue.add("ranjeet");

I want to remove "world" from it.

我想从中删除“世界”。

回答by Menios

Queue Interface

队列接口

The queue interface only allows you to remove elements from the head of the queue. See the description of the API at:

队列接口只允许你从队列的头部移除元素。请参阅以下位置的 API 说明:

https://docs.oracle.com/javase/7/docs/api/java/util/Queue.html#remove()

https://docs.oracle.com/javase/7/docs/api/java/util/Queue.html#remove()

The whole purpose of the queue data structure is to push items to the tail and remove them from the head (as a real queue works).

队列数据结构的全部目的是将项目推到尾部并将它们从头部移除(就像真正的队列一样)。

You should use a different data structure / collection object type.

您应该使用不同的数据结构/集合对象类型。

Another option would be to remove all the items of the queue and put them in another queue (except the item you want to remove).

另一种选择是删除队列中的所有项目并将它们放入另一个队列(除了要删除的项目)。

Finally, another would be to make your own queue implementation adding the extra method.

最后,另一种方法是让您自己的队列实现添加额外的方法。

LinkedList

链表

I linkedlist is an implementation that implements the Queue interface but it also implements other interfaces.

我链表是一个实现 Queue 接口的实现,但它也实现了其他接口。

You could use the method:

您可以使用以下方法:

remove(Object o)Removes the first occurrence of the specified element from this list, if it is present. If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

remove(Object o)从此列表中删除第一次出现的指定元素(如果存在)。如果此列表不包含该元素,则它保持不变。更正式地,删除具有最低索引 i 的元素,使得 (o==null ? get(i)==null : o.equals(get(i))) (如果存在这样的元素)。如果此列表包含指定的元素(或等效地,如果此列表因调用而更改),则返回 true。

https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#remove(java.lang.Object)

https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#remove(java.lang.Object)

You could change your code to:

您可以将代码更改为:

LinkedList<String> queue = new LinkedList<>();

OR

或者

List<String> queue = new LinkedList<>();

Why are you using a Queue/LinkedList?

为什么要使用队列/链表?

The main question is why are you using a Queue/Linkedlist? It seems that also a basic list could be suitable for what you want. If you want to remove intermediate items, a linkedlist is not the most suitable.

主要问题是你为什么使用队列/链表?似乎基本列表也可能适合您想要的内容。如果要删除中间项,链表不是最合适的。

LinkedList

链表

Implements both interfaces of List and Queue. See:

实现了 List 和 Queue 两个接口。看:

回答by GhostCat

Correct. Queuedoes not have a method to remove a specificobject (at a certain index, or that is equal to some provided argument):

正确的。Queue没有删除特定对象的方法(在某个索引处,或者等于某个提供的参数):

Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. ... Whatever the ordering used, the headof the queue is that element which would be removed by a call to remove() or poll(). In a FIFO queue, all new elements are inserted at the tailof the queue. Other kinds of queues may use different placement rules. Every Queue implementation must specify its ordering properties.

队列通常但不一定以 FIFO(先进先出)方式对元素进行排序。...无论使用何种顺序,队列的头部都是将通过调用 remove() 或 poll() 删除的元素。在 FIFO 队列中,所有新元素都插入到队列的尾部。其他类型的队列可能使用不同的放置规则。每个 Queue 实现都必须指定其排序属性。

The reason for that is simple: Queueis not a "random" access data structure. It only supports removing elements from head resp. tail; depending on the implementation.

原因很简单:Queue不是“随机”访问的数据结构。它只支持从 head resp 中删除元素。尾巴; 取决于实施。

In that sense the answer is one of:

从这个意义上说,答案是以下之一:

  • you either have to "pop" all entries of the queue up to the one to remove, to then addthem back
  • you change the underlying data structure to something that allows "random" remove actions
  • 您要么必须将队列中的所有条目“弹出”到要删除的条目,然后再将它们添加回来
  • 您将底层数据结构更改为允许“随机”删除操作的内容

And please note: your code shows a potential solution. Simply change it to:

请注意:您的代码显示了一个潜在的解决方案。只需将其更改为:

List<String> strings = new LinkedList<>();

and all of a sudden you can remove()objects by index or "by value".

突然之间,您可以remove()按索引或“按值”对象。

To my knowledge, there is nointerfaces that combines both interfaces (List and Queue). But of course, there are implementations, such as LinkedListthat actually do that.

据我所知,没有结合两个接口(列表和队列)的接口。但是,当然,有一些实现,例如LinkedList实际上这样做的。