Java Collection Framework - Detailed Guide
1. Overview of Java Collection Framework
- Introduced in Java 2 (JDK 1.2).
- Provides reusable data structures and algorithms.
- Located in the package: java.util.
- Interfaces define data structures, and classes implement them.
2. Java Collection Framework Hierarchy
JCF is broadly divided into three main types of interfaces:
1. Collection Interface (extends Iterable)
- List, Set, Queue
2. Map Interface (does not extend Collection)
- Map
3. Utility Classes
- Collections, Arrays
3. Core Interfaces in Java Collection Framework
- Iterable: Root interface, allows iteration using for-each loop.
- Collection: Base interface for all collections, extends Iterable.
- List: Ordered collection (array-like), allows duplicates.
- Set: Unordered collection, no duplicates allowed.
- Queue: FIFO (First In, First Out) structure, used for processing elements sequentially.
- Deque: Double-ended queue, allows insertion/removal from both ends.
- Map: Stores key-value pairs, keys are unique.
4. Important Implementations of JCF
A. List Implementations
- ArrayList: Dynamic array, fast read operations, slow insert/delete in the middle.
- LinkedList: Doubly linked list, fast insert/delete, slower random access.
- Vector: Synchronized version of ArrayList.
- Stack: LIFO (Last In, First Out) structure, extends Vector.
B. Set Implementations
- HashSet: Unordered, allows unique elements, uses hashing.
- LinkedHashSet: Maintains insertion order, slower than HashSet.
- TreeSet: Sorted set (ascending order), implemented using a Red-Black Tree.
C. Queue Implementations
- PriorityQueue: Elements are sorted based on priority (natural order or custom comparator).
- ArrayDeque: Efficient double-ended queue (faster than Stack).
D. Map Implementations
- HashMap: Unordered, key-value pairs, allows null keys and values.
- LinkedHashMap: Maintains insertion order.
- TreeMap: Sorted by key (natural order), uses Red-Black Tree.
- Hashtable: Synchronized version of HashMap, does not allow null keys or values.
5. Utility Classes in JCF
- Collections Class (Helper Methods):
- Collections.sort(list)
- Collections.reverse(list)
- Collections.max(list)
- Collections.min(list)
- Collections.unmodifiableList(list)
6. Key Interview Questions
A. Conceptual Questions
1. What is the difference between ArrayList and LinkedList?
2. How does HashMap work internally?
3. What are the differences between HashSet and TreeSet?
4. Why is Hashtable synchronized while HashMap is not?
5. What are the advantages of ArrayDeque over Stack?
B. Coding Questions
1. Implement a custom LinkedList in Java.
2. Find the first non-repeating character in a string using LinkedHashMap.
3. Given an array of numbers, find duplicate elements using HashSet.
4. Implement a LRU Cache using LinkedHashMap.
7. Summary
- List: Ordered, allows duplicates (ArrayList, LinkedList, Vector, Stack).
- Set: Unique elements (HashSet, LinkedHashSet, TreeSet).
- Queue: FIFO and priority-based (PriorityQueue, ArrayDeque).
- Map: Key-value pairs (HashMap, TreeMap, LinkedHashMap, Hashtable).
- Utility classes: Collections and Arrays.