I've told my students to write
List<MyClass> myList = new ArrayList<MyClass>();
instead of
ArrayList<MyClass> myList = new ArrayList<MyClass>();
as this allows them to switch to any Class that implements List without having to change more code.
This does always make sense, except if you need methods from ArrayList or LinkedList. But which methods does ArrayList / LinkedList offer that List doesn't have?
ArrayList has:
- Object clone()
- void ensureCapacity(int minCapacity)
- void removeRange(int fromIndex, int toIndex)
- void trimToSize()
LinkedList has:
- void addFirst(E e)
- void addLast(E e)
- Object clone()
- Iterator
descendingIterator() - E getFirst()
- E getLast()
- boolean offer(E e)
- boolean offerFirst(E e)
- boolean offerLast(E e)
- E peek()
- E peekFirst()
- E peekLast()
- E poll()
- E pollFirst()
- E pollLast()
- E pop()
- void push(E e)
- E removeFirst()
- boolean removeFirstOccurrence(Object o)
- E removeLast()
- boolean removeLastOccurrence(Object o)