Java Collections Framework
The Java Collections Framework (JCF) is a unified architecture used to store, manipulate, and process groups of objects efficiently. It provides a standardized set of interfaces, classes, and algorithms that simplify data handling in Java applications.
Before diving deeper into specific data structures such as ArrayList, HashSet, or HashMap, it is important to understand the overall structure of the Collections Framework.
All major collection classes are available in the java.util package, which provides ready-to-use implementations for common data storage patterns.
Developers use the Collections Framework to:
- Store large amounts of data
- Search and retrieve elements quickly
- Sort and manipulate collections
- Write cleaner and more maintainable code
Tip:
Think of the Collections Framework as a toolbox. Interfaces define what operations are possible, while classes provide the actual implementation of those operations.
Core Components of the Java Collections Framework
The framework is mainly divided into interfaces and implementation classes.
- Interfaces define the structure and behavior of collections.
- Classes implement these interfaces and provide working functionality.
Understanding Key Collection Interfaces
1. List Interface
The List interface represents an ordered collection of elements. Items are stored in the order they are inserted, and duplicates are allowed.
Key Characteristics
- Maintains insertion order
- Allows duplicate values
- Supports index-based access
Example Program: Managing a Task List
import java.util.ArrayList;
import java.util.List;
public class TaskManager {
public static void main(String[] args) {
List<String> tasks = new ArrayList<>();
tasks.add("Complete report");
tasks.add("Attend meeting");
tasks.add("Review code");
tasks.add("Complete report"); // duplicate allowed
System.out.println("Today's Tasks:");
for (int i = 0; i < tasks.size(); i++) {
System.out.println((i + 1) + ". " + tasks.get(i));
}
}
}
When to Use List
Use a List when:
- The order of elements matters
- Duplicate values are acceptable
- You want to access elements by index
2. Set Interface
The Set interface represents a collection that does not allow duplicate elements. It is commonly used when uniqueness is important.
Key Characteristics
- No duplicate values allowed
- Typically does not guarantee insertion order (depending on implementation)
- Efficient for membership checks
Example Program: Tracking Unique Course Codes
import java.util.HashSet;
import java.util.Set;
public class CourseRegistry {
public static void main(String[] args) {
Set<String> courseCodes = new HashSet<>();
courseCodes.add("CS101");
courseCodes.add("DS202");
courseCodes.add("AI303");
courseCodes.add("CS101"); // duplicate ignored
System.out.println("Available Courses:");
for (String code : courseCodes) {
System.out.println(code);
}
}
}
When to Use Set
Use a Set when:
- You need unique elements only
- Duplicate entries must be prevented
- Fast lookup operations are required
3. Map Interface
The Map interface stores data in key–value pairs. Each key must be unique, while values may be duplicated.
Maps are useful when you need to associate one value with another.
Key Characteristics
- Stores key-value pairs
- Keys must be unique
- Fast data retrieval using keys
Example Program: Storing Employee Salaries
import java.util.HashMap;
import java.util.Map;
public class EmployeeSalarySystem {
public static void main(String[] args) {
Map<Integer, Double> salaryDatabase = new HashMap<>();
salaryDatabase.put(1001, 55000.0);
salaryDatabase.put(1002, 62000.0);
salaryDatabase.put(1003, 58000.0);
salaryDatabase.put(1004, 71000.0);
System.out.println("Employee Salaries:");
for (Map.Entry<Integer, Double> entry : salaryDatabase.entrySet()) {
System.out.println("Employee ID: " + entry.getKey() +
" Salary: $" + entry.getValue());
}
}
}
When to Use Map
Use a Map when:
- Data needs to be accessed using a unique key
- You are storing paired information (ID → Name, Product → Price, etc.)
Choosing the Right Collection
Selecting the correct collection type depends on your program’s requirements.
Use List when:
- Element order is important
- Duplicate elements are allowed
- Index-based access is needed
Use Set when:
- You must avoid duplicate values
- Only unique elements should be stored
Use Map when:
- Data must be stored as key-value pairs
- Fast lookups using keys are required