Hacker News new | ask | show | jobs
by krzyk 1341 days ago
List lacks simple methods to access the head and tail. There are multiple times I wanted to get the tail element but had to do two calls (get size and get(size-1)). Having first()/last() methods would make it easy and more readable.
3 comments

On the other hand the way the interfaces are layered is not ideal, because the implications of e.g. `removeFirst` and `removeLast` for an arraylist are very different from a linked list.

Or getLast on an arraylist (or doubly linked list) versus singly linked.

Nit: "tail" usually means "everything except the head", not "last element". I got a little confused there for a moment reading your comment.
Doesn't java libs have a deq - a double-ended queue?
Yes, Java has java.util.Deque [1]. The purpose of this JEP is to add a more generic interface over the ordered collection types, which all have slightly different names for these operations.

[1] https://docs.oracle.com/en/java/javase/17/docs/api/java.base...