DevNotes
Support
Collection Framework · Chapter 01

Introduction to Java Collections Framework

Why collections exist, the problems with arrays, Collection vs Collections, and the overall hierarchy.

◷ 5 min read§ 15 sections? 8 interview questions{ } 17 code examples

#What is a Collection?

Imagine your company has millions of users: User1, User2, User3 … User1000000.

Can we create variables like these?

Java
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.
Java
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:

Today50 employees
Tomorrow100 employees
Next Month500 employees

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

Java
int[] arr = new int[5];

Maximum size = 5. You cannot store a 6th element — you need to create another array. Collections solve this problem:

Java
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:

Java
int[] arr = {10, "ABC"};   // Compilation Error

Collections (without Generics) can store heterogeneous objects:

Java
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
Framework means Java already provides ready-made implementations

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
Java
Collection<String> c;

Collections

  • Utility class
  • Contains static helper methods
Java
Collections.sort(list);
Collections.reverse(list);
Collections.shuffle(list);
Collections.max(list);
Collections.min(list);

Collection vs Collections vs Arrays

CollectionCollectionsArrays
InterfaceUtility ClassUtility Class
Stores ObjectsStatic MethodsStatic 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
Map has a separate hierarchy

#Why Doesn't Map Extend Collection?

  • Collection stores objects.
  • Map stores 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:

Java
for (Object obj : collection)

This works because Collection extends Iterable. Iterable provides iterator().

Java
Iterable<String> itr;

Iterator

Iterator allows traversal.

Java
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

Java
ArrayList list = new ArrayList();
list.add(10);
list.add("ABC");

You need casting:

Java
Integer i = (Integer) list.get(0);

After Generics

Java
ArrayList<Integer> list = new ArrayList<>();
list.add(10);

Compile-time type safety.

Diamond Operator

Java 7 introduced the Diamond Operator. Instead of:

Java
ArrayList<Integer> list = new ArrayList<Integer>();

use:

Java
ArrayList<Integer> list = new ArrayList<>();

Cleaner and shorter.

#Iterable Example

Java
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:

LibraryJava
BooksCollection (the objects)
Library RulesFramework
Shelf TypesList, Set, Queue
Shelf ImplementationsArrayList, 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 → Interface
  • Collections → 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?
  • Collection is the root interface for most collection types.
  • Iterable sits above Collection and enables iteration.
  • Map has 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.
  • Collection is an interface.
  • Collections is a utility class.
  • Map is not part of the Collection hierarchy.
  • Iterable enables the enhanced for-each loop.
  • Generics provide compile-time type safety.