Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.ArrayDeque remove and removeFirst

Tags:

java

I looked java.util.ArrayDeque class remove() and removeFirst() method and I saw remove() calls removeFirst() and two method makes the same operation. Why there is two method for the same operation?

like image 705
karlkeller Avatar asked Nov 15 '25 09:11

karlkeller


2 Answers

This is because the Queue interface requires implementing classes to have a function remove and the Deque interface requires implementing classes to have a removeFirst function, and the ArrayDeque implements a Deque (Double ended queue), which is an entended version of the Queue interface. Therefore, ArrayDeque has to implement the functions of both interfaces. In my opinion, the removeFirst function is there for clarity reasons, because the remove function would be kind of ambiguous with regards to its name.

like image 124
Tobi042 Avatar answered Nov 17 '25 09:11

Tobi042


They are defined by different interfaces.

Queue.remove() was defined in Java 5.0 and remove the "next" element of the queue.

Deque.removefirst() was defined in Java 6 and it removes the first element of the deque. It is similar to removeLast();

The way these are implement in ArrayDeque is that remove() of the next is actually the same as removeFirst().

If you you are wondering which one to use, I suggest using the one you beleiev is clearest.

like image 26
Peter Lawrey Avatar answered Nov 17 '25 08:11

Peter Lawrey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!