Nearby lessons

82 of 125

Java - Collections Framework

📌 What You Will Learn
  • What the Collection Framework is and why we need it
  • List, Set, Map and Queue — the four big families
  • Choosing the right collection for the right job
  • Comparable vs Comparator
  • The internal working of HashMap (interview favourite)

Collections Framework is a core concept of the Java language. This lesson explains The Problem Before Collections, Collection vs Collections — Do Not Confuse and The Family Tree in Simple Form with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

The Problem Before Collections

An array has a fixed size. What if you need to store 5 numbers now and 50 later? Arrays cannot grow. Managing such groups by hand was painful.

Java's Collection Framework (in java.util) gives us ready-made, growable, powerful classes to store and manage groups of objects. It also gives sorting, searching, and thread-safe versions — all free.

In simple words: A collection is a ready-made, growable container for objects — you get add, remove, sort and search for free, instead of managing fixed-size arrays by hand.

Think of it as your wardrobe: instead of one fixed-size box, you get many types of cabinets — a shelf (List), a storage bag that rejects duplicates (Set), and a labelled box for name-to-value pairs (Map).

Collection vs Collections — Do Not Confuse

NameWhat it isExample
CollectionAn interface — the root of the collection family (List, Set, Queue).Collection<String> c = new ArrayList<>();
CollectionsA utility class with static helper methods for sorting, searching, reversing.Collections.sort(list);
In simple words: `Collection` is the interface — the family name for List, Set and Queue; `Collections` is a toolbox of static helper methods like sort() and reverse(). One is a blueprint, the other is a utility class.

The Family Tree in Simple Form

Example03
JCode Cell
1Collection (interface)
2 |-- List -> ArrayList, LinkedList, Vector
3 |-- Set -> HashSet, LinkedHashSet, TreeSet
4 |-- Queue -> PriorityQueue, ArrayDeque
5 
6Map (interface) -> HashMap, LinkedHashMap, TreeMap, Hashtable

Choosing the Right Collection — Quick Guide

Your needChoose
Growable list with indexes, duplicates OKArrayList
No duplicatesHashSet
No duplicates + sorted orderTreeSet
Key-value pairs, fastestHashMap
Key-value pairs, sorted by keyTreeMap
Key-value pairs, insertion orderLinkedHashMap
Key-value pairs, thread safe (legacy)Hashtable
FIFO processing (queue)ArrayDeque / PriorityQueue
Sorting custom objectsComparable or Comparator

Array vs Collection — Question and Answer

The classic material starts the whole chapter with this question: we already have arrays, why do we need collections?

PointArrayCollection
SizeFixed — cannot growDynamically growable
Type of elementsSame type (homogeneous)Any type (heterogeneous) by default
Extra methodsNo built-in sort/searchReady-made: sort, reverse, search
PerformanceFasterA little slower (but powerful)
Predefined?A language featureClasses in java.util
Example05
JCode Cell
1Student[] std = new Student[3];
2std[3] = new Student(); // ArrayIndexOutOfBoundsException!
3 
4ArrayList<Student> list = new ArrayList<>();
5list.add(new Student()); // no size limit - grows automatically
📝 Key Takeaways
  • Collection is an interface; Collections is a utility class with static helpers.
  • List = ordered, allows duplicates. Set = no duplicates. Map = key-value pairs. Queue = FIFO.
  • ArrayList is the everyday list; HashMap is the everyday map.
  • HashMap works on hashCode + buckets; collisions chain, then become trees (Java 8+).
  • Comparable (compareTo) is the natural order; Comparator (compare) gives custom order.
  • Always override equals() and hashCode() together for your own objects.
  • Collections.sort() and Collections.reverse() are the handy helpers.

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1