Your Success, Our Mission!
6000+ Careers Transformed.
The Java Collections Framework (JCF) provides a unified architecture for storing, manipulating, and retrieving groups of objects efficiently.
Introduced in Java 2 (JDK 1.2), the framework standardizes data structure handling by offering ready-to-use classes and interfaces such as List, Set, Map, and Queue.
Collections replace primitive arrays with dynamic, resizable, and type-safe data containers, improving performance and maintainability.
They handle everything from ordered sequences to unique element sets and key-value mappings, enabling developers to implement complex algorithms without manually managing data storage.
The framework resides under java.util and supports both generic types (for compile-time type safety) and iterators (for sequential traversal).
Common collection operations include insertion, deletion, searching, sorting, and iteration.
Understanding when to use each collection type — based on order, uniqueness, and access pattern — is critical for writing efficient code.
import java.util.*;
public class CollectionIntro {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
list.add("C++");
System.out.println("List: " + list);
Set<String> set = new HashSet<>(list);
System.out.println("Set (unique elements): " + set);
Map<Integer, String> map = new HashMap<>();
map.put(1, "Apple");
map.put(2, "Banana");
map.put(3, "Cherry");
System.out.println("Map: " + map);
}
}
Output:
List: [Java, Python, C++]
Set (unique elements): [C++, Java, Python]
Map: {1=Apple, 2=Banana, 3=Cherry}
Explanation:
A List maintains order and allows duplicates, while a Set automatically removes duplicates and doesn’t maintain insertion order.
A Map stores key-value pairs, providing fast lookup by key. These three are the cornerstones of the Java Collections Framework.
Managing large data collections dynamically (e.g., user lists, transaction records).
Implementing caches, dictionaries, and lookup tables.
Removing duplicates from datasets.
Structuring hierarchical or relational data in applications.
Interface | Implementation | Ordered | Duplicates | Thread-Safe | Example Use |
List | ArrayList, LinkedList | Yes | Yes | No | Ordered collections |
Set | HashSet, TreeSet | No | No | No | Unique elements |
Map | HashMap, TreeMap | No | Keys unique | No | Key-value storage |
Queue | LinkedList, PriorityQueue | Yes | Yes | No | Task scheduling |
Lists in Java are ordered collections that allow duplicate elements. Each element has an index, enabling random access and modifications.
The most common implementations are:
Sets, on the other hand, are unordered collections that ensure all elements are unique.
The major implementations include:
Choosing between these depends on performance requirements, ordering needs, and data uniqueness constraints.
import java.util.*;
public class ListSetExample {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>(Arrays.asList(5, 3, 8, 3, 1));
System.out.println("ArrayList: " + numbers);
Set<Integer> uniqueNumbers = new HashSet<>(numbers);
System.out.println("HashSet (unique values): " + uniqueNumbers);
Set<Integer> sortedNumbers = new TreeSet<>(numbers);
System.out.println("TreeSet (sorted unique): " + sortedNumbers);
}
}
Output:
ArrayList: [5, 3, 8, 3, 1]
HashSet (unique values): [1, 3, 5, 8]
TreeSet (sorted unique): [1, 3, 5, 8]
Explanation:
The ArrayList keeps duplicates, HashSet removes duplicates, and TreeSet automatically sorts unique elements.
This shows how different collection types serve distinct purposes with minimal code changes.
ArrayList: Storing ordered data like student lists or logs.
LinkedList: Implementing queues and stacks.
HashSet: Removing duplicates in search results.
TreeSet: Maintaining sorted leaderboards or rankings.
Collection | Data Structure | Ordered | Duplicates | Time Complexity (Add/Search) |
ArrayList | Dynamic Array | Yes | Yes | O(1)/O(n) |
LinkedList | Doubly Linked List | Yes | Yes | O(1)/O(n) |
HashSet | Hash Table | No | No | O(1)/O(1) |
TreeSet | Red-Black Tree | Yes (Sorted) | No | O(log n)/O(log n) |
Maps store data in key-value pairs, allowing fast retrieval of values using keys.
Unlike other collections, Map does not extend Collection but stands as a separate interface under java.util.
Major implementations include:
Iteration through a Map can be done using entry sets, key sets, or lambda expressions. Java 8’s forEach() method and enhanced Iterator API allow elegant and efficient traversal.
Map is heavily used in databases, caching, and any situation requiring key-based lookups.
import java.util.*;
public class MapIteration {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 90);
scores.put("Bob", 85);
scores.put("Charlie", 95);
System.out.println("Scores Map: " + scores);
// Iterating using entrySet
for (Map.Entry<String, Integer> entry : scores.entrySet()) {
System.out.println(entry.getKey() + " → " + entry.getValue());
}
// Using forEach (Java 8+)
scores.forEach((key, value) -> System.out.println("Lambda: " + key + " scored " + value));
}
}
Output:
Scores Map: {Alice=90, Bob=85, Charlie=95}
Alice → 90
Bob → 85
Charlie → 95
Lambda: Alice scored 90
Lambda: Bob scored 85
Lambda: Charlie scored 95
Explanation:
This example demonstrates iterating over a Map using both traditional and functional-style loops.
Maps enable efficient lookup and manipulation of related key-value pairs.
Map Type | Ordering | Null Keys | Performance | Example |
HashMap | Unordered | One allowed | O(1) | General purpose |
LinkedHashMap | Insertion order | One allowed | O(1) | LRU cache |
TreeMap | Sorted by keys | Not allowed | O(log n) | Sorted mapping |
Top Tutorials
CNN in Deep Learning 2026
A beginner-friendly guide to CNNs: understand deep learning essentials, create Python-based models, and explore advanced applications.
Breaking The Limits: Scaling Databases with MySQL Partitioning
Learn MySQL partitioning with examples. Improve query performance, scalability, and data management using RANGE, LIST, HASH, KEY, and composite techniques.
ML in Action: Hands-On Guide to Deploying and Serving Models
Learn model deployment and serving—from concepts to real-world architectures, tools, APIs, containers, and cloud workflows for production-ready ML.
All Courses (6)
Master's Degree (2)
Fellowship (2)
Certifications (2)