Lesson 36: Introduction to the Java Collections Framework
The Java Collections Framework (JCF) is a unified architecture for representing and manipulating collections (groups) of objects. It offers a set of interfaces and classes to handle data structures efficiently.
1. Why Use Collections?
While arrays are fast, they have a fixed size. Collections are dynamic (resize automatically) and provide sophisticated data structures and algorithms (sorting, searching).
2. Core Interfaces of the JCF
All collection classes implement one of these root interfaces:
A. Collection Interface (The Root)
It is the base interface for most collection types. It defines basic methods like add(), remove(), size(), and contains().
B. List Interface
- Ordered collection (elements maintain their insertion order).
- Allows duplicate elements.
- Indexed access (like arrays).
- Implementations:
ArrayList,LinkedList,Vector.
C. Set Interface
- Unordered collection.
- Does not allow duplicate elements.
- Implementations:
HashSet,TreeSet,LinkedHashSet.
D. Map Interface
- Does not extend the
Collectioninterface (it's slightly separate). - Stores data as key-value pairs.
- Keys must be unique; values can be duplicated.
- Implementations:
HashMap,TreeMap,LinkedHashMap.
3. Generics Requirement
Notice that collection types require Generics (e.g., <String>, <Integer>) to ensure type safety. We will cover Generics in detail soon, but for now, understand that we tell the collection what type of object it will store.