Home > Software design >  Deque inherits from Queue; why is there no elementFirst()? To avoid duplication, shouldn't Java
Deque inherits from Queue; why is there no elementFirst()? To avoid duplication, shouldn't Java

Time:09-12

Deque inherits from Queue; to avoid duplication of methods wouldn't it be better for the Java Creators to rename Queue methods to explicitly say position?

For instance, if Queue has an addLast() then Dequeue only needs to have an addFirst(). Instead, now it has an addFirst() and also an add() and addLast().

Also, to be consistent, there should have been an elementFirst(). Instead, there is a getFirst().

It still can be done because methods and classes are deprecated all the time.

There is a somewhat different question here: Deque's methods and the methods inherited from Queue

CodePudding user response:

A Queue, as defined in Java, does not have any order for the elements it contains. It is just a Collection. See the following sentence from the documentation:

Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner.

Because of this there is no "position" available for the elements it contains. So there is no method for getting the position of an element in a queue.

  •  Tags:  
  • java
  • Related