Ranjeet Kumar yadav
Ranjeet Kumar yadav

Reputation: 182

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

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

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

I want to remove "world" from it.

Upvotes: 4

Views: 31918

Answers (3)

Sihat Afnan
Sihat Afnan

Reputation: 882

queue.remove(obj) won't work when you've instantiated your Queue obj as a Linked List. You've to use

queue.remove(idx) 

Upvotes: 0

Menelaos
Menelaos

Reputation: 25725

Queue Interface

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

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.

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).

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:

Upvotes: 4

GhostCat
GhostCat

Reputation: 140427

Correct. Queue does not have a method to remove a specific object (at a certain index, or that is equal to some provided argument):

Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. ... Whatever the ordering used, the head of 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 tail of the queue. Other kinds of queues may use different placement rules. Every Queue implementation must specify its ordering properties.

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

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 add them 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".

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

Upvotes: 0

Related Questions