Interview Q&A · Java · 3 questions in this topic
The List Interface and Its Implementations — Java Interview Q&A
Ordered indexed lists and the classes that implement them, plus the List-specific operations interviews love to ask about. Part of Collections Framework · 294 questions in the Java bank.
What is the List interface?
Answer
List is an ordered collection that remembers insertion order and allows access by integer index, much like an array. Unlike sets, it permits duplicate elements and more than one null in most implementations. Its contract adds positional operations on top of the Collection operations, so you can insert, read, replace, or remove the element at a given position. Common implementations are ArrayList, LinkedList, Vector, and the concurrent CopyOnWriteArrayList.
Which methods are specific to List?
Answer
List adds index-based methods such as add at an index, get by index, set to replace by index, and remove by index. It adds search methods indexOf and lastIndexOf, where indexOf returns the index of the first occurrence and lastIndexOf returns the index of the last occurrence. It also provides subList for a live view of a range, and listIterator for bidirectional traversal. Note that the older source text swaps indexOf and lastIndexOf, but the correct behaviour is first occurrence for indexOf and last occurrence for lastIndexOf.
Key points
- 1addAll with an index inserts a whole collection at a position.
- 2subList returns a view, so changes to the sublist show up in the original list.
- 3listIterator extends Iterator with previous, indexes, add, and set operations.
💡 If asked, state the correction clearly: indexOf finds the first match, lastIndexOf finds the last match.
What are the List implementations?
Answer
The classic implementations are ArrayList, LinkedList, and the legacy Vector with its Stack subclass. Modern Java adds CopyOnWriteArrayList for concurrent read-heavy use. Choose ArrayList for fast random access, LinkedList when you need cheap insertion and removal in the middle plus deque operations, Vector only when you must support legacy synchronized code, and CopyOnWriteArrayList when many threads read but few threads write.
Key points
- 1ArrayList: resizable array, best default choice for get-heavy work.
- 2LinkedList: doubly linked nodes, implements both List and Deque.
- 3Vector and Stack: synchronized legacy classes, largely replaced by ArrayList and Deque.
- 4CopyOnWriteArrayList: snapshot iterator for concurrency, expensive writes.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission