> Java Collection Framework (JCF) is a UNIFIED ARCHITECTURE that provides a set of classes and interfaces to store, retrieve, manipulate, and communicate aggregate data efficiently.
> It was designed to bring all commonly used data structures under one standardized structure, making them easier to use, manage, and extend.
┌────────────────────┐
| Iterable (interface)
└────────▲───────────┘
│
extends
│
┌────────────────────────┐
| Collection (interface) |
└───────▲────▲────▲──────┘
│ │ │
extends │ extends
│
┌──────────────── ┐ ┌───────────────┐ ┌────────────────────────┐
| List (interface)| | Set (interface)| | Queue (interface) |
└──────▲──────────┘ └──────▲─────────┘ └────────▲────────────── ┘
│ │ │
implements implements implements
│ │ │
┌────────────────────┐ ┌────────────────────┐ ┌────────────────────────────┐
| ArrayList (class) | | HashSet (class) | | PriorityQueue (class) |
|LinkedList (class) | | LinkedHashSet | | ArrayDeque (class) |
| Vector (class) | | TreeSet (class) | | |
| Stack (class) | └────────────────────┘ | LinkedList (class) |
└────────────────────┘ | (also implements Deque) |
────────────────────────────┘
LinkedList implements List & Deque (double-ended queue)
Before JCF, Java had multiple unrelated classes (like Vector, Hashtable, etc.) for handling collections.
Java unified these under one architecture in JCF.
> Provides standard interfaces (List, Set, Queue, Map).
> Includes utility methods for sorting, searching, and manipulation.
> Supports type safety through generics.
Iterable <E> is the root interface in the collection hierarchy. It allows an object to be traversed (iterated) through a collection, enabling sequential access to its elements.
Enables traversal of elements using:
(1) iterator() method - To iterate through elements
(2) forEach() > Enhanced loop using lambda
(3) spliterator() – For parallel iteration
Extends Iterable
Common methods: add(), remove(), contains(), size(), isEmpty(), clear()
order of insertion is maintained.
Allows duplicates and nulls.
Access via index.
Implementations: ArrayList, LinkedList, Vector, Stack.
No duplicates
No guaranteed order (depends on implementation)
Implementations: HashSet, LinkedHashSet, TreeSet
FIFO structure
Used for task scheduling, buffers
Implementations: PriorityQueue, ArrayDeque, LinkedList
Key-value pair storage
Implementations: HashMap, LinkedHashMap, TreeMap, Hashtable
Generics ensures type safety - only allows elements of the specified type.
List<String> list = new ArrayList<>();
list.add("Java"); // Allowed
list.add(123); // Compile-time errorWithout generics, collections return Object, meaning we’ll need to manually type cast each element.
This increases chances of runtime errors like ClassCastException.
Generics provide type safety and eliminate the need for casting.
List list = new ArrayList(); // Raw type (No generics)
list.add(100);
int num = (int) list.get(0); // Manual casting neededCollections store OBJECTS, not primitive types(int,double).
Collections use wrapper classes (Integer, Double, Character, etc.) because collections work with objects and they require reference types.
Java provides wrapper classes for each primitive type, like:
• Integer for int
• Character for char
• Double for double
List<int> list = new ArrayList<>(); // Error - primitives not supported
List<Integer> list = new ArrayList<>(); // Wrapper classJava uses interfaces (like List, Set, Queue) to define what a class must do, and concrete classes (like ArrayList, HashSet, LinkedList) to actually do it.
Interfaces declare abstract methods (method signatures only, no logic).
Concrete classes implement these interfaces and provide the actual logic for those methods.
This promotes loose coupling and flexibility in how we work with collections.
This is runtime polymorphism in action — where a parent reference (interface) points to a child object (class) and calls overridden methods.
List<String> list = new ArrayList<>();
list.add("Hello"); // List defines it, ArrayList implements it
Though list is of type List, the add() method that gets executed is from ArrayList.