Collections Framework
Collections Framework Overview
Analyze the Java Collections Framework architecture, distinguishing legacy structures from modern standard interfaces.
Interview: Focuses on hierarchy root interfaces (Collection, Map), legacy class problems (Vector, Hashtable), and structural benefits.
The Java Collections Framework (JCF) is a unified architecture for representing and manipulating collections. It provides high-performance interfaces, implementations, and algorithms that reduce programming effort and promote software reuse.
Unified Interfaces
Defines standard contracts like Collection, List, Set, Queue, and Map so implementations are interchangeable.
Legacy Classes
Older classes (e.g. Vector, Hashtable) were retrofitted in JDK 1.2 to implement JCF interfaces, though their heavy synchronization overhead remains.
Algorithms Library
The static Collections class provides polymorphic algorithms (sorting, searching, shuffling) operating on collection interfaces.
Core Architecture
The JCF separates the collection structure into two main branches:
- java.util.Collection: The root interface for lists, sets, and queues containing elements.
- java.util.Map: A key-value association mapping that does not inherit from
Collectiondue to key-value structural differences.
Common Pitfalls
- Using legacy classes: Choosing
VectororHashtablein single-threaded environments, which causes unnecessary synchronization lock overhead. - Assuming collection types: Coding against concrete implementations (e.g.
ArrayList list = new ArrayList()) instead of interface targets (List list = new ArrayList()), violating encapsulation.
Best Practices
- Code to Interfaces: Always declare collections using their parent interface types (e.g.
List,Set) to maintain structural flexibility. - Understand complexity boundaries: Know time complexities (
O(1)vsO(N)vsO(log N)) of each collection type to avoid structural performance bottlenecks.
Interview-Relevant Information
Q1: Why does Map interface not extend Collection?
Answer: A Map manages key-value pairings rather than single values. Its operations (like put(K, V) and get(Object)) are incompatible with the element-focused operations of the Collection interface (like add(E)).
Q2: What is the main problem with legacy collections like Vector?
Answer: Legacy collections have synchronized methods. This incurs an unnecessary locking overhead for single-threaded executions. Modern collections are unsynchronized by default, requiring explicit concurrent wrappers if needed.
Quick Checklist
Can you draw the JCF interface hierarchy, explain why Map is a separate hierarchy root, and name two differences between legacy and modern collections? If yes, you understand collections framework overview.
Use Cases
Building modular business layers where database result sets are converted to JCF models.
Designing data processing utilities using unified interfaces for lists and maps.
Common Mistakes
Declaring variables with concrete classes instead of using parent interfaces.
Using synchronized Vector classes in non-thread-safe environments.