Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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
7  
+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
1  
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
6  
There isn't a first() method either, so why should there be a last() method? –  Peter Taylor Apr 19 '11 at 20:37
4  
@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
1  
@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
show 2 more comments

7 Answers

up vote 10 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
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

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

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
4  
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

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>();

        l.add("A");
        l.add("B");
        l.add("Z");

        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
share|improve this answer
1  
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
1  
.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
1  
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
1  
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
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
1  
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
2  
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

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 http://download.oracle.com/javase/tutorial/collections/interfaces/collection.html

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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