Search code examples
javacollectionsinterfacequeuedeque

Why Deque delcare on method with exactly the same functionality as the Queue interface which it extends?


I can’t understand why although Deque interface extends queue interface,Deque interface duplicates Queue methods. For example: 1. Why Queue interface declares on getFirst method and not “satisfied” with “element” which does the same thing (and is inherited from Queue interface). If we will look on LinkedList class (which implements Deque interface), element method implementation does nothing beside calling “getFirst” method. Why Deque declares on getFirst method? 2.method peek (inherited from Queue interface) which have exactly (seems like copy paste) the same implementation as peekFirst method? Why Deque declares on peek method? I can find another example but I think my case is clear but i think it's enough


Solution

  • A Deque does the same thing as a Queue and also has additional methods to access elements of the Deque from the opposite end. Hence the Double Ended Queue. So the Deque interface simply extends the Queue interface and makes use of those methods to facilitate using it as a Queue.

    So if you had a Deque and tried wanted to do add(), it would work like addFirst(). But addFirst() isn't supported by Queue. So, imo, it is for convenience. The API also tells you which methods are equivalent. They just don't tell you why.

    Check this out for more information. Double Ended Queue