Introduction to Java Collections Framework
Why collections exist, the problems with arrays, Collection vs Collections, and the overall hierarchy.
#What is a Collection?
Imagine your company has millions of users: User1, User2, User3 … User1000000.
Can we create variables like these?
User u1;
User u2;
User u3;
// ...Impossible. Instead, Java introduced Collections. A Collection is simply:
An object that can store a group of objects as a single unit.
List<String> names = new ArrayList<>();
names.add("Rahul");
names.add("Ankit");
names.add("John");Instead of creating hundreds of variables, you create one collection.
#Why Collections?
Suppose you are writing an Employee Management System. Employees keep changing:
Arrays become difficult to manage because their size is fixed. Collections automatically grow and shrink. That is why Java introduced the Collections Framework.
#Problems with Arrays
1. Fixed Size
int[] arr = new int[5];Maximum size = 5. You cannot store a 6th element — you need to create another array. Collections solve this problem:
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);The list grows automatically.
2. Homogeneous
Arrays can store only one type: int[], String[], Employee[]. You cannot do:
int[] arr = {10, "ABC"}; // Compilation ErrorCollections (without Generics) can store heterogeneous objects:
ArrayList list = new ArrayList();
list.add(10);
list.add("ABC");
list.add(true);3. No Built-in Methods
Arrays provide very few utility methods, so you need manual coding. Collections provide methods like:
add(), remove(), contains(), clear(), sort(), replaceAll(), retainAll(), removeIf(), stream()
Huge advantage.
4. Memory Wastage
Suppose you create new int[1000]; but only 10 elements are used — 990 spaces are wasted. Collections allocate memory dynamically.
5. Difficult Searching
With arrays you need loops (for(...)). Collections provide contains() and indexOf().
#What is the Collection Framework?
The Collection Framework is:
A set of interfaces and classes used to store and manipulate groups of objects.
- Collection Framework
- InterfacesList · Set · Queue
- ClassesArrayList · LinkedList · HashSet · TreeSet · PriorityQueue
No need to reinvent everything.
#What is a Framework?
Examples:
- Java Collections
- Spring Framework
- Hibernate Framework
- JUnit
#Collection vs Collections
This is an extremely important interview question.
Collection
- Interface
- Package:
java.util - Stores a group of objects
Collection<String> c;Collections
- Utility class
- Contains static helper methods
Collections.sort(list);
Collections.reverse(list);
Collections.shuffle(list);
Collections.max(list);
Collections.min(list);Collection vs Collections vs Arrays
| Collection | Collections | Arrays |
|---|---|---|
| Interface | Utility Class | Utility Class |
| Stores Objects | Static Methods | Static Methods |
add() | sort() | sort() |
remove() | reverse() | binarySearch() |
#Java Collections Hierarchy
- Iterable
- Collection
- List
- ArrayList
- LinkedList
- Vector
- Stack
- Set
- HashSet
- LinkedHashSet
- TreeSet
- Queue
- PriorityQueue
- LinkedList
- Map
- HashMap
- LinkedHashMap
- TreeMap
- Hashtable
- WeakHashMap
- IdentityHashMap
- EnumMap
- ConcurrentHashMap
#Why Doesn't Map Extend Collection?
Collectionstores objects.Mapstores key → value pairs.
Completely different architecture. Hence, Map has its own hierarchy.
#Root Interfaces
There are four major interfaces: Collection, Map, Iterator, Iterable. Everything else extends these.
Iterable
Java 5 introduced the enhanced for loop:
for (Object obj : collection)This works because Collection extends Iterable. Iterable provides iterator().
Iterable<String> itr;Iterator
Iterator allows traversal.
Iterator<Integer> itr = list.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}#Benefits of Collections Framework
- Dynamic Size
- Reusable Classes
- High Performance
- Standard API
- Sorting
- Searching
- Thread-safe Alternatives
- Generics Support
- Stream API Support
- Easy Maintenance
#Generics + Collections
Before Java 5
ArrayList list = new ArrayList();
list.add(10);
list.add("ABC");You need casting:
Integer i = (Integer) list.get(0);After Generics
ArrayList<Integer> list = new ArrayList<>();
list.add(10);Compile-time type safety.
Diamond Operator
Java 7 introduced the Diamond Operator. Instead of:
ArrayList<Integer> list = new ArrayList<Integer>();use:
ArrayList<Integer> list = new ArrayList<>();Cleaner and shorter.
#Iterable Example
List<String> names = new ArrayList<>();
names.add("Rahul");
names.add("Ankit");
names.add("John");
for (String name : names) {
System.out.println(name);
}Internally, Java converts this into an Iterator.
#Real-life Analogy
Imagine a Library:
| Library | Java |
|---|---|
| Books | Collection (the objects) |
| Library Rules | Framework |
| Shelf Types | List, Set, Queue |
| Shelf Implementations | ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue |
#Common Interview Questions
Q1What is the Java Collection Framework?
A set of interfaces and classes that provides an architecture to store and manipulate groups of objects dynamically.
Q2Why not Arrays?
- Fixed Size
- No Built-in Methods
- Difficult Insertion & Deletion
- No Dynamic Resizing
- Less Flexible
Q3Difference between Collection and Collections?
Collection→ InterfaceCollections→ Utility Class
Q4Does Map extend Collection?
No. Map is a separate hierarchy because it stores key-value pairs instead of individual elements.
Q5Which is the root interface?
Collectionis the root interface for most collection types.Iterablesits aboveCollectionand enables iteration.Maphas its own hierarchy.
Q6Why was Iterable introduced?
To support the enhanced for-each loop (for (T item : collection)) through the iterator() method.
#Chapter Summary
- A Collection stores a group of objects as a single unit.
- Arrays have fixed size; Collections are dynamic.
- The Java Collections Framework provides reusable interfaces and implementations.
Collectionis an interface.Collectionsis a utility class.Mapis not part of the Collection hierarchy.Iterableenables the enhanced for-each loop.- Generics provide compile-time type safety.