Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is for ordered collections e.g. java.util.List. Why did the language designers not include a last method? The only reasons I can think of are:

  • ambiguity when the collection is empty (return null or throw exception)
  • API bloat

Any other reasons?

share|improve this question

closed as primarily opinion-based by Jarrod Roberson, MichaelT, gnat, Giorgio, GlenH7 Dec 9 '14 at 20:06

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

+1 for the sheer number of times that I've had to write collection.get(collection.size() - 1). – jprete Apr 19 '11 at 19:51
Why not write your own utility class that has this method, and any other method you might want to use with collections? – Mahmoud Hossam Apr 19 '11 at 19:54
There isn't a first() method either, so why should there be a last() method? – Peter Taylor Apr 19 '11 at 20:37
@Peter urgh the only thing worse than not implementing getLast() is implementing it with a hack like get(-1). – Alb Apr 19 '11 at 23:12
@Alb I guess "hack" is in the eye of the beholder :) At this point in time, I appreciate python's -1 syntax. Of course, if you ask me again sometime in the future or in the past, I may feel exactly as you do. – three-cups May 10 '12 at 3:51
up vote 12 down vote accepted

API bloat is probably the answer. From my experience the only time I've needed this functionality a Queue or Stack was the correct data structure for the job having the appropriate method.

share|improve this answer
Guess you are partially correct, but if Java supported get(-1) to retrieve from the end of a list it would do what the OP wanted without adding API bloat. My answer as such has drawn unexplained downvotes. – user949300 Dec 7 '14 at 23:09
I think a bigger answer is that Java's initial failure (repeated in .NET, and still ongoing in the latter case) to support default interface methods meant that having interfaces include a member which 99% of implementations would handle the same way would impose extra work on all implementations for the benefit of the few that would implement them differently. – supercat Mar 20 '15 at 13:22

a last() method is just as easy as list.get(list.size()-1), just like there is no first() method or fifth() method. It isn't that hard to synthesize and is a specialization. You can also reverse() the list and list.get(0) which will give the last item. Things that are easy to do, usually don't get their own specialized methods.

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

public class Main
    public static void main(String[] args)
        final List<String> l = new ArrayList<String>();


        System.out.println("l.get(0) = " + l.get(0));
        System.out.println("l.get(l.size()-1) = " + l.get(l.size() - 1));

results in the following output

l.get(0) = A
l.get(l.size()-1) = Z

it is also presumptuous to assume that everything that implements the List interface actually has the concept of last() anything.

share|improve this answer
It would be clearer in both cases though – Anto Apr 19 '11 at 19:56
Note that the size() method may not be able to answer if the collection is not yet fully populated. – user1249 Apr 20 '11 at 10:37
.size() will return the current size .size()-1 will still be the last element regardless, how would it know if it was fully populated or not? – Jarrod Roberson Apr 20 '11 at 18:15
By the specification, a List can contain more than Integer.MAX_VALUE elements, in which case size() returns Integer.MAX_VALUE, so .size()-1 is not the perfect way to implement last() (though such a large list is very unlikely and I wonder how such a list would implement toArray()...) – user281377 May 9 '12 at 9:52
C# does have a .First & .Last method. Reversing a list just to get the last item isn't exactly efficient either. – Carra Feb 6 '13 at 13:06

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 interested in the last element you might consider to use the java.util.Queue interface's peek() method. LinkedList implements Queue.

share|improve this answer

Basically you either have to ask for the size() for a for-loop or iterate it by asking for its iterator and use it in a while or do-loop. Use the one suitable for your purpose.

The iterator knows at a given point if there are more entries and allows you to get the next one if there is. You then repeat until "more entries?" fails.

See the "Traversing Collections" section at

share|improve this answer

protected by MichaelT Dec 7 '14 at 3:39

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.