The Java Collection Framework is a fundamental part of Java, and interviewers often test candidates on their understanding of collections, their implementations, and their use cases. Below are some common Java Collection Framework interview questions along with explanations and examples.
1. What is the Java Collection Framework?
The Java Collection Framework is a unified architecture for representing and manipulating collections of objects. It includes:
Interfaces: Define the behavior of collections (e.g., List, Set, Map).
Implementations: Concrete classes that implement the interfaces (e.g., ArrayList, HashSet, HashMap).
Algorithms: Methods for searching, sorting, and manipulating collections.
2. What are the main interfaces in the Collection Framework?
The main interfaces are:
Collection: The root interface for all collections (except Map).
List: An ordered collection that allows duplicates (e.g., ArrayList, LinkedList).
Set: A collection that does not allow duplicates (e.g., HashSet, TreeSet).
Map: A collection of key-value pairs (e.g., HashMap, TreeMap).
Queue: A collection designed for holding elements prior to processing (e.g., PriorityQueue, LinkedList).
3. What is the difference between List, Set, and Map?
Feature
List
Set
Map
Order
Ordered (by insertion)
Unordered (except LinkedHashSet)
Unordered (except LinkedHashMap)
Duplicates
Allows duplicates
No duplicates
No duplicate keys
Implementation
ArrayList, LinkedList
HashSet, TreeSet
HashMap, TreeMap
4. What is the difference between ArrayList and LinkedList?
Feature
ArrayList
LinkedList
Underlying Data Structure
Dynamic array
Doubly linked list
Access Time
O(1) for random access
O(n) for random access
Insertion/Deletion Time
O(n) for add/remove at arbitrary positions
O(1) for add/remove at beginning/end
Use Case
Frequent read operations
Frequent add/remove operations
5. What is the difference between HashSet and TreeSet?
Feature
HashSet
TreeSet
Order
Unordered
Sorted (natural order or custom comparator)
Performance
O(1) for add, remove, contains
O(log n) for add, remove, contains
Implementation
Uses HashMap internally
Uses TreeMap internally
Use Case
Fast access, no ordering needed
Sorted data, custom ordering
6. What is the difference between HashMap and TreeMap?
Feature
HashMap
TreeMap
Order
Unordered
Sorted (natural order or custom comparator)
Performance
O(1) for put, get, remove
O(log n) for put, get, remove
Implementation
Uses hashing
Uses Red-Black Tree
Use Case
Fast access, no ordering needed
Sorted data, custom ordering
7. What is the difference between HashMap and Hashtable?
Feature
HashMap
Hashtable
Synchronization
Not synchronized
Synchronized
Null Keys/Values
Allows one null key and multiple null values
Does not allow null keys or values
Performance
Faster
Slower due to synchronization
Legacy
Part of Java Collection Framework
Legacy class
8. What is the difference between Comparable and Comparator?
Feature
Comparable
Comparator
Interface
java.lang.Comparable
java.util.Comparator
Method
compareTo()
compare()
Usage
Defines natural ordering
Defines custom ordering
Example
Collections.sort(list)
Collections.sort(list, comparator)
Example of Comparable:
class Student implements Comparable<Student> {
private String name;
private int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int compareTo(Student other) {
return this.age - other.age; // Natural ordering by age
}
}
Example of Comparator:
import java.util.Comparator;
class NameComparator implements Comparator<Student> {
@Override
public int compare(Student s1, Student s2) {
return s1.getName().compareTo(s2.getName()); // Custom ordering by name
}
}
9. What is the fail-fast and fail-safe behavior in collections?
Fail-Fast: Iterators throw a ConcurrentModificationException if the collection is modified while iterating (e.g., ArrayList, HashMap).
Fail-Safe: Iterators do not throw exceptions if the collection is modified while iterating (e.g., CopyOnWriteArrayList, ConcurrentHashMap).
10. What is the difference between Iterator and ListIterator?