0

I am not asking how to find it, that is already answered in this other question:

But just going deeper into data-structures in Java, I have found out that the LinkedList implementation has a getLast() method that is not implemented (I suppose for some reason) in the ArrayList implementation. I could not find any other similar question nor post on Internet explaining it, so I decided to ask it here.

We may agree that it is not elegant the current way of getting the last element from an ArrayList, and usually this implementation is more widely used than LinkedList because it performs better in a wider range of scenarios, as discussed here.

Does anybody know why ArrayList does not implement that method?

Edit: I have edited my question to avoid confusion and opinion based answers. The answer below from Andreas is the one I was looking for, based on facts and references.

Reubems
  • 51
  • 7
  • 1
    This a question you should take to the person that designed the `List` interface. – Turing85 Aug 31 '19 at 15:24
  • And couldn't that interface be extended as they did for backwards compatibility with the types Date and Calendar when introducing the new Date and Time API in Java 8? I mean to the .from(...) methods they added for mutual conversion. I believe it would not be so complicated to do it so, and don't believe I am the first one wondering it.. – Reubems Aug 31 '19 at 15:33
  • 2
    You are welcome to open a [JEP](http://openjdk.java.net/jeps/0). – Turing85 Aug 31 '19 at 15:38
  • If it's ArrayList you want to use, you can always write your own class, extend ArrayList and write a `getLast()` method. Then reuse that class in your code! Problem solved – Soutzikevich Aug 31 '19 at 17:07
  • Please note that it's at least simple to create a helper method: http://www.softsmithy.org/softsmithy-lib/lib/2.0/docs/site/apidocs/org/softsmithy/lib/util/Lists.html#getLast-java.util.List- – Puce Aug 31 '19 at 19:42

2 Answers2

4

The Collection classes generally don't implement any public methods that aren't specified by an interface.

ArrayList does have one method named trimToSize() that isn't specified by an interface, but that is a very implementation-specific method.

LinkedList doesn't have any methods that isn't specified by an interface.

The reason LinkedList has a getLast() is shown in the javadoc:

Specified by:
getLast in interface Deque<E>

Deque has most of the same methods as List does. The main difference is that List has methods for accessing elements by index position, while Deque has methods for accessing first / last element.

If you want to work with a list-like collection structure where access to the last element is simple, code to the Deque interface, not the List interface, and use ArrayDeque instead of ArrayList.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Thank you so much for your explanation Andreas, this was actually the type of answer I was looking for, because I could not understand that decision. I did not know the type ArrayDeque, but I believe is very interesting and will explore it further. I appreciate a lot your explanation. – Reubems Sep 01 '19 at 18:14
  • 1
    @Reubems `Deque` is a wastly overlooked member of the Java Collections Framework. Most people just use `List`, because `List` is *good enough* for what they need, when `Deque` would have been a better choice. I'm guilty of that too, but at least I'm aware of its existence, and I have used it, just not of often as I probably should have. – Andreas Sep 01 '19 at 19:15
1

You can access an ArrayList last element with a simple ArrayListName.get(ArrayListName.size() - 1); because you can access an ArrayList element directly with it's index, however in a linked list you have to iterate through every element before you're able to access the last element, this is [most probably] why the engineers created a method to access the last element of a linked list.

Daniel_Kamel
  • 610
  • 8
  • 29
  • That's not the reason at all. The reason [`LinkedList`](https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html) implements the [`getLast()`](https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html#getLast--) method is that it has to, since it implements the [`Deque`](https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html) interface, which [`ArrayList`](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html) doesn't. `LinkedList` doesn't implement any public method that isn't declared by an interface. – Andreas Aug 31 '19 at 16:39