Take the 2-minute tour ×


  • API bloat

Any other reasons?

share|improve this question
show 2 more comments

7 Answers

up vote 10 down vote accepted


share|improve this answer
add comment


import java.util.ArrayList;
import java.util.List;

public class Main
    public static void main(String[] args)



results in the following output

l.get(0) = A
l.get(l.size()-1) = Z
share|improve this answer
add comment

The java.util.LinkedList defines the getLast() and getFirst() methods. Unfortunately these methods are not defined in one of it's interfaces, so you have to use the LinkedList type. If you are only interessted in the last element you might consider to use the java.util.Queue interface's peek() method. LinkedList implements Queue ;)

share|improve this answer
add comment




share|improve this answer
add comment

Because Gosling was a LISP programmer (and always expected lisp processing to go by 'cdr')?

Kind of a flip answer, but what are you going to use a generic list for? If you're really going to be accessing the last element a lot, do you really want a generic data type like List? You'd be signing up for whatever performance horriblenesses there are in, say, a singly-linked list.

I'd think List was a minimal-functionality type, not a maximal-functionality type.

share|improve this answer
I doubt that James Gosling himself designed the collections API. As far as I know most of that has been designed by Joshua Bloch. –  Jesper May 9 '12 at 10:13
add comment

As others have said, it's probably because they didn't want to bloat the interface.

You could use java.util.Deque which is implemented by java.util.LinkedList or java.util.ArrayDeque. Your trading in random access, though. Deque has getFirst() and getLast() and you'd still be programming against an interface.

share|improve this answer
add comment

What meaning does last() (or first(), for that matter) have with a Set or any other possible future collection other than a List?

And in practice, how often are you likely to call that method on a List?

share|improve this answer
I debated that second sentence. I'm sure that some people want to look at the last element in a list and ignore all the others. But I suspect that most of them shouldn't be using a List –  Anon Apr 19 '11 at 20:50
The question specifically said ordered collections. –  Alb Apr 19 '11 at 21:03
The question isn't just about List. last() and first() would make sense for SortedSet and other ordered collections. –  Andres F. May 9 '12 at 12:24
add comment

Your Answer


