By WebNest Studio Engineering Team
Java - Core Tutorial
Collections Framework Overview
Before the Collections Framework, Java developers stored groups of objects in plain arrays or in ad hoc classes like Vector and Hashtable, each with its own inconsistent set of methods. The Java Collections Framework, introduced in Java 1.2, replaced that inconsistency with a single, unified architecture: a small set of interfaces that define what a data structure can do, and a rich set of implementing classes that define how it does it efficiently.
The core idea is separation of interface from implementation. Code written against the List interface works identically whether the underlying object is an ArrayList or a LinkedList, which means you can change the internal data structure later — for performance reasons — without touching the rest of your program.
Why it matters
Almost every Java program holds groups of objects: rows from a database, items in a cart, requests waiting to be handled. The Collections Framework gives all of these the same small set of interfaces, so choosing the right one is mostly a matter of answering three questions: does order matter, are duplicates allowed, and do you look things up by a key? Interviewers ask about collections more than any other part of the standard library.
The Interface Hierarchy
At the root sits the Iterable interface, which guarantees anything implementing it can be used in a for-each loop. Collection extends Iterable and is the parent of the three main branches used for storing individual elements. Map is deliberately kept separate, because it stores key-value pairs rather than single elements, and does not extend Collection.
Collection→List(ordered, allows duplicates) → ArrayList, LinkedList, VectorCollection→Set(no duplicates) → HashSet, LinkedHashSet, TreeSet (via SortedSet/NavigableSet)Collection→Queue(FIFO-style processing) → LinkedList, PriorityQueue, andDeque→ ArrayDequeMap(separate hierarchy, key-value pairs) → HashMap, LinkedHashMap, TreeMap, Hashtable
Why the Framework Exists
The framework gives every collection a predictable, shared vocabulary: methods like add(), remove(), contains(), and size() behave consistently across implementations, and generic algorithms in the Collections utility class (Collections.sort(), Collections.reverse(), Collections.max()) work on any List or Collection regardless of its concrete type. This is what lets you swap a HashMap for a TreeMap, or an ArrayList for a LinkedList, by changing one line of code.
Choosing the Right Collection
Picking a collection is really answering three questions: does order matter, are duplicates allowed, and do you need key-value lookup? A List preserves insertion order and allows duplicates; a Set forbids duplicates; a Map associates unique keys with values; a Queue/Deque models processing order (first-in-first-out, or double-ended). Choosing correctly up front avoids awkward workarounds later.
Example
The same Collection reference type working with two different implementations
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class FrameworkDemo {
public static void main(String[] args) {
List<String> names = new ArrayList<>(); // could be swapped for LinkedList
names.add("Asha");
names.add("Ravi");
names.add("Meera");
printAll(names); // works identically for any List implementation
List<String> linked = new LinkedList<>(names);
printAll(linked);
}
static void printAll(List<String> list) {
for (String name : list) {
System.out.print(name + " ");
}
System.out.println();
}
}
Asha Ravi Meera
Asha Ravi Meera
Common Mistakes
- Assuming Map is a Collection — it is a separate hierarchy because it stores pairs, not single elements.
- Declaring variables with the concrete type (ArrayList) instead of the interface type (List), which defeats the purpose of programming to an interface.
- Picking a collection based on habit ("I always use ArrayList") instead of the actual access pattern needed.
Exercise
The list below contains repeated names. Build a collection that holds each name once, in alphabetical order, then print how many distinct names there are and the collection itself.
Expected output: 3 then [Asha, Ravi, Zoya]
import java.util.List;
public class DistinctNames {
public static void main(String[] args) {
List<String> names = List.of("Ravi", "Asha", "Zoya", "Asha", "Ravi");
// TODO: create a collection with no duplicates, kept in sorted order
// TODO: print its size, then print the collection
}
}
Show hints
- A
Setrejects duplicates, and aTreeSetalso keeps its elements sorted. - Most collections have a constructor that takes another collection:
new TreeSet<>(names).
Show solution
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class DistinctNames {
public static void main(String[] args) {
List<String> names = List.of("Ravi", "Asha", "Zoya", "Asha", "Ravi");
Set<String> distinct = new TreeSet<>(names);
System.out.println(distinct.size()); // 3
System.out.println(distinct); // [Asha, Ravi, Zoya]
}
}
Quick quiz
1. Which of these interfaces does not extend Collection?
- A. List
- B. Set
- C. Queue
- D. Map
Show answer
D. Map stores key-value pairs and has its own hierarchy.
2. You need to keep elements in the order they were added and allow duplicates. Which interface fits?
- A. List
- B. Set
- C. Map
- D. SortedSet
Show answer
A. A List is ordered by position and allows duplicate elements.
3. Why is List<String> names = new ArrayList<>(); preferred over ArrayList<String> names = new ArrayList<>();?
- A. It runs faster
- B. The rest of the code depends only on the interface, so the implementation can be changed in one place
- C. ArrayList cannot be used as a variable type
- D. It uses less memory
Show answer
B. Code written against List works unchanged if the implementation later becomes a LinkedList or an unmodifiable list.
Interview questions
What is the difference between Collection and Collections?
Show answer
Collection is an interface, the root of the List, Set and Queue hierarchy. Collections is a utility class of static methods that work on collections, such as sort, reverse, max and unmodifiableList.
What are the main interfaces of the Collections Framework and how do they differ?
Show answer
List is an ordered sequence that allows duplicates and access by index. Set holds no duplicates. Queue holds elements waiting to be processed, usually first-in-first-out, and Deque allows adding and removing at both ends. Map stores key-value pairs with unique keys and is not a Collection.
What is the difference between an array and an ArrayList?
Show answer
An array has a fixed length and can hold primitives or objects. An ArrayList grows as elements are added, holds only objects (primitives are boxed), and provides methods such as add, remove and contains. An ArrayList uses an array internally and copies it to a larger one when it is full.
Key Points to Remember
- The Collections Framework unifies data structures under a small set of interfaces: Collection, List, Set, Queue, and the separate Map hierarchy.
- Programming against interfaces (List, Set, Map) rather than concrete classes makes implementations swappable.
- The Collections utility class provides algorithms (sort, reverse, max) that work across all implementations.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs plain Java 17. Frameworks such as Spring, and databases, are not available here. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering