Problem:
What are Java List types? What are their methods and what are their properties?
Solution:
Here are java.util.List implementations and their properties:
- ArrayList: fast iteration, random access
- LinkedList: insertion order, fast insertion/deletion
- Vector: fast iteration, random access, synchronized
Methods common for List implementations:
- boolean add(E): adds element to the list
- boolean add(int index, E): insert element E to the specified position
- boolean contains(Object): checks whether the list contains specified element
- E get(int index): returns an element from the specified position on list
- int indexOf(Object): returns index of first occurrence of given element or -1 if not found
- Iterator<E> iterator(): returns list iterator in correct order
- E remove(int index): removes element from specified position on list and returns it
- boolean remove(Object): removes the first occurrence of given object
- int size(): returns a number of elements in the list
- Object[] toArray(): returns an array containing all list’s elements in proper order (from the first to the last)
- <T> T[] toArray(T[]): returns an array containing all list’s elements in proper order, with type T equal the formal type parameter.
It’s important to know the implementations and these methods if you want to take Oracle Certified Programmer exam.