I recently had a discussion with a collegue why the List interface in Java doesn\'t have a head()
and tail()
method.
In order to implement
head()
is provided via list.iterator().next(), list.get(0), etc.
It is only reasonable to provide tail() if the list is doubly linked with a tail pointer, or based on an array, etc., Neither of these aspects is specified for the List interface itself. Otherwise it could have O(N) performance.
peekLast method is already defined in Deque interface.
Moreover, It's obligatory for deque to have such a functionality. So, there's no point to define it in List or any other interface.
It's just convenient to split the functionality. If you need random access then you should implement List. If you need to access the tail efficiently then you should implement Deque. You can easily implement both of them (LinkedList does this, actually).