4

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?

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

Comments

6

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.