Collections Interview Questions
Comprehensive collections interview questions and answers for Java. Prepare for your next job interview with expert guidance.
Questions Overview
1. What are the differences between ArrayList and LinkedList?
Moderate2. What is the difference between `HashMap` and `HashTable` in Java?
Advanced3. What is the `Iterator` interface in Java?
Moderate4. What is a `Comparator` in Java?
Moderate5. What is the difference between `Set` and `List` in Java?
Moderate1. What are the differences between ArrayList and LinkedList?
ModerateArrayList is based on a dynamic array and allows fast random access but slow insertion and deletion (except at the end). LinkedList is based on a doubly linked list, offering fast insertions and deletions, but slower random access.
2. What is the difference between `HashMap` and `HashTable` in Java?
Advanced`HashMap` is not synchronized and allows one `null` key and multiple `null` values, whereas `HashTable` is synchronized (thread-safe) and doesn't allow `null` keys or values.
3. What is the `Iterator` interface in Java?
ModerateThe `Iterator` interface provides a way to iterate over a collection of objects, allowing removal of elements during iteration. It has methods `hasNext()`, `next()`, and `remove()`. It's part of Java's `java.util` package.
4. What is a `Comparator` in Java?
ModerateThe `Comparator` interface provides a method to define custom ordering of objects. It can be used to sort collections of objects based on specific attributes by overriding the `compare()` method.
5. What is the difference between `Set` and `List` in Java?
Moderate`Set` is a collection that does not allow duplicate elements and is unordered. `List` allows duplicates and maintains the order of elements. Examples of `Set` include `HashSet` and `TreeSet`, while `List` includes `ArrayList` and `LinkedList`.