Java collection-interview
Q1. What is the Java Collections Framework? Why is it used?
The Java Collections Framework (JCF) is a set of classes and interfaces that implement commonly used data structures like List, Set, Map, and Queue.
It is used to store, manipulate, and retrieve data efficiently. Benefits include:
- Reusability of data structures
- Reduced development time
- Built-in algorithms like sorting and searching
Q2. What is the difference between List, Set, and Map?
- List → Ordered collection, allows duplicates. Example: ArrayList, LinkedList.
- Set → Unordered collection, does not allow duplicates. Example: HashSet, TreeSet.
- Map → Stores key-value pairs, keys must be unique. Example: HashMap, TreeMap.
Q3. What is the difference between ArrayList and LinkedList?
- ArrayList → Backed by a dynamic array, fast for accessing elements, slower for insertion/deletion in the middle.
- LinkedList → Backed by a doubly linked list, faster for insertion/deletion, slower for random access.
Q4. What is the difference between HashMap and TreeMap?
- HashMap → Stores entries in an unordered manner; allows one null key and multiple null values; faster (O(1) operations).
- TreeMap → Stores entries in sorted order of keys; does not allow null key; slower than HashMap (O(log n) operations).