Java
Explain Java Collections Framework with Diagrams and Code Examples
By Utility Zone · 2025-11-06T11:34:07.252046
The Java Collections Framework (JCF) is a unified architecture for representing and manipulating groups of objects in Java. It provides interfaces, classes, and algorithms for storing, accessing, and manipulating collections of data efficiently. The Collections Framework is located in the java.util package.123

Complete Java Collections Framework hierarchy showing interfaces and implementations with their relationships
What is the Collections Framework?
The Collections Framework provides a set of interfaces and implementations for handling collections of objects. It standardizes the way collections are represented, allowing you to work with different collection types through common interfaces.12
Core Interfaces:21
- Iterable - Root interface enabling iteration over elements4
- Collection - Base interface for most collections4
- List - Ordered collections allowing duplicates2
- Set - Unique elements only, unordered by default2
- Queue - FIFO (First-In-First-Out) collections2
- Map - Key-value pair collections2
Detailed Overview of Core Collection Types
1. List Interface
List represents an ordered collection where elements can be accessed by position. Lists allow duplicate elements and maintain insertion order.2
Implementations:
ArrayList - Resizable array implementation:56
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Apple"); // Duplicates allowed
System.out.println(fruits.get(0)); // Fast access: O(1)
System.out.println(fruits.size()); // Output: 3
Performance: O(1) for get/set, O(n) for add/remove (except at end)65
LinkedList - Doubly-linked list implementation:56
List<String> names = new LinkedList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
// Can be used as Queue too
names.removeFirst(); // Removes "Alice" - O(1) operation
Performance: O(n) for get/set, O(1) for add/remove at beginning/end65
Vector - Legacy, synchronized version of ArrayList:56
List<String> syncedList = new Vector<>(); // Thread-safe but slower
syncedList.add("Item1");
syncedList.add("Item2");
Performance: Same as ArrayList but with thread-safety overhead65
When to Use Each List Implementation:56
- ArrayList: Default choice for most use cases; fast random access
- LinkedList: When frequent add/remove at beginning/end of list
- Vector: Only for legacy code requiring synchronization (use Collections.synchronizedList() instead)
2. Set Interface
Set represents a collection of unique elements. Duplicate elements are not allowed.27
Implementations:
HashSet - Hash table-based implementation:87
Set<Integer> numbers = new HashSet<>();
numbers.add(10);
numbers.add(20);
numbers.add(10); // Duplicate - will be ignored
System.out.println(numbers); // Output: [10, 20] (order not guaranteed)
System.out.println(numbers.contains(10)); // Output: true
Performance: O(1) for add, remove, contains78 Ordering: No specific order87
TreeSet - Sorted tree-based implementation:78
Set<Integer> sortedNumbers = new TreeSet<>();
sortedNumbers.add(30);
sortedNumbers.add(10);
sortedNumbers.add(20);
System.out.println(sortedNumbers); // Output: [10, 20, 30] - sorted order
Performance: O(log n) for add, remove, contains87 Ordering: Elements are sorted in natural order78
LinkedHashSet - Maintains insertion order:2
Set<String> cities = new LinkedHashSet<>();
cities.add("New York");
cities.add("London");
cities.add("Tokyo");
// Iteration order is insertion order
for (String city : cities) {
System.out.println(city);
}
When to Use Each Set Implementation:87
| Requirement | Use |
|---|---|
| Fast operations, order doesn't matter | HashSet |
| Need sorted elements | TreeSet |
| Need insertion order maintained | LinkedHashSet |
3. Map Interface
Map represents key-value pairs where each key maps to exactly one value.2
Implementations:
HashMap - Hash table-based, unordered:9
Map<String, Integer> studentGrades = new HashMap<>();
studentGrades.put("Alice", 85);
studentGrades.put("Bob", 92);
studentGrades.put("Charlie", 78);
System.out.println(studentGrades.get("Alice")); // Output: 85
System.out.println(studentGrades.containsKey("Bob")); // Output: true
Performance: O(1) for get, put, remove9 Ordering: No specific order9
TreeMap - Sorted tree-based implementation:9
Map<String, Integer> sortedGrades = new TreeMap<>();
sortedGrades.put("Zoe", 88);
sortedGrades.put("Alice", 85);
sortedGrades.put("Mike", 90);
// Iteration order is sorted by key
for (String name : sortedGrades.keySet()) {
System.out.println(name + ": " + sortedGrades.get(name));
}
// Output: Alice: 85, Mike: 90, Zoe: 88
Performance: O(log n) for get, put, remove9
LinkedHashMap - Maintains insertion order:2
Map<String, String> config = new LinkedHashMap<>();
config.put("database", "MySQL");
config.put("host", "localhost");
config.put("port", "3306");
// Iteration order is insertion order
for (Map.Entry<String, String> entry : config.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
ConcurrentHashMap - Thread-safe implementation:1011
Map<String, Integer> threadSafeMap = new ConcurrentHashMap<>();
threadSafeMap.put("count", 0);
// Safe for concurrent access from multiple threads
Iterating Through Collections
Using Iterator
Iterator is used to traverse any collection in forward direction only:12
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
Iterator<String> iterator = fruits.iterator();
while (iterator.hasNext()) {
String fruit = iterator.next();
System.out.println(fruit);
if (fruit.equals("Banana")) {
iterator.remove(); // Safe removal during iteration
}
}
Using ListIterator
ListIterator allows bidirectional traversal and modification:1314
List<String> names = new ArrayList<>();
names.add("Shyam");
names.add("Rajat");
names.add("Paul");
names.add("Tom");
ListIterator<String> listIterator = names.listIterator();
// Forward iteration
System.out.println("Forward:");
while (listIterator.hasNext()) {
System.out.println(listIterator.next());
}
// Backward iteration
System.out.println("\nBackward:");
while (listIterator.hasPrevious()) {
System.out.println(listIterator.previous());
}
Output:
Forward:
Shyam
Rajat
Paul
Tom
Backward:
Tom
Paul
Rajat
Shyam
Enhanced For-Each Loop
The simplest way to iterate:12
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
for (int num : numbers) {
System.out.println(num);
}
Collections Utility Class
The Collections class provides static utility methods for operating on collections:151617
Sorting:1617
List<String> fruits = new ArrayList<>();
fruits.add("Mango");
fruits.add("Apple");
fruits.add("Banana");
Collections.sort(fruits); // Ascending order
System.out.println(fruits); // Output: [Apple, Banana, Mango]
Collections.sort(fruits, Collections.reverseOrder()); // Descending
System.out.println(fruits); // Output: [Mango, Banana, Apple]
Shuffling:15
List<Integer> numbers = new ArrayList<>();
numbers.addAll(Arrays.asList(1, 2, 3, 4, 5));
Collections.shuffle(numbers); // Random order
System.out.println(numbers); // Output: [3, 1, 5, 2, 4] (or any random order)
Reversing:16
List<String> names = new ArrayList<>();
names.addAll(Arrays.asList("Alice", "Bob", "Charlie"));
Collections.reverse(names);
System.out.println(names); // Output: [Charlie, Bob, Alice]
Searching:16
List<Integer> numbers = Arrays.asList(10, 20, 30, 40, 50);
int index = Collections.binarySearch(numbers, 30);
System.out.println("Index of 30: " + index); // Output: Index of 30: 2
Frequency:16
List<String> fruits = Arrays.asList("Apple", "Banana", "Apple", "Orange", "Apple");
int count = Collections.frequency(fruits, "Apple");
System.out.println("Frequency of Apple: " + count); // Output: 3
Real-World Example: Student Grade Management
import java.util.*;
public class StudentGradeManager {
public static void main(String[] args) {
// Store students and their grades
Map<String, Double> gradeBook = new LinkedHashMap<>();
gradeBook.put("Alice", 85.5);
gradeBook.put("Bob", 92.0);
gradeBook.put("Charlie", 78.5);
gradeBook.put("Diana", 88.0);
// Get unique grades
Set<Double> uniqueGrades = new HashSet<>(gradeBook.values());
System.out.println("Unique Grades: " + uniqueGrades);
// Sort students by name
List<String> sortedNames = new ArrayList<>(gradeBook.keySet());
Collections.sort(sortedNames);
System.out.println("\nStudents (alphabetical):");
for (String name : sortedNames) {
System.out.println(name + ": " + gradeBook.get(name));
}
// Find highest grade
double maxGrade = Collections.max(gradeBook.values());
System.out.println("\nHighest Grade: " + maxGrade);
// Find students with highest grade
for (Map.Entry<String, Double> entry : gradeBook.entrySet()) {
if (entry.getValue() == maxGrade) {
System.out.println("Student with highest grade: " + entry.getKey());
}
}
// Filter students above 85
System.out.println("\nStudents with grade > 85:");
for (Map.Entry<String, Double> entry : gradeBook.entrySet()) {
if (entry.getValue() > 85) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
}
Output:
Unique Grades: [85.5, 92.0, 78.5, 88.0]
Students (alphabetical):
Alice: 85.5
Bob: 92.0
Charlie: 78.5
Diana: 88.0
Highest Grade: 92.0
Student with highest grade: Bob
Students with grade > 85:
Alice: 85.5
Bob: 92.0
Diana: 88.0
Performance Comparison Table
| Operation | ArrayList | LinkedList | HashSet | TreeSet | HashMap | TreeMap |
|---|---|---|---|---|---|---|
| get() | O(1) | O(n) | N/A | N/A | O(1) | O(log n) |
| add() | O(n)* | O(1) | O(1) | O(log n) | O(1) | O(log n) |
| remove() | O(n)* | O(1) | O(1) | O(log n) | O(1) | O(log n) |
| contains() | O(n) | O(n) | O(1) | O(log n) | O(1) | O(log n) |
| Ordered | Yes | Yes | No | Yes | No | Yes |
| Duplicates | Yes | Yes | No | No | Keys-No | Keys-No |
*O(1) amortized for add at end
Best Practices
- Use List interface for ordered collections:
List<String> list = new ArrayList<>();2 - Use Set for unique elements:
Set<Integer> set = new HashSet<>();2 - Use Map for key-value pairs:
Map<String, Integer> map = new HashMap<>();2 - Prefer ArrayList over Vector for single-threaded applications56
- Use TreeSet/TreeMap when sorted data is needed89
- Use ConcurrentHashMap in multi-threaded environments1011
- Use Collections utility methods for common operations1617
- Specify initial capacity when creating collections to avoid resizing overhead6
The Java Collections Framework is essential for writing efficient, maintainable Java applications. Understanding the hierarchy, characteristics, and performance implications of each collection type enables you to choose the right tool for your specific needs.132 <span style="display:none">181920</span>
<div align="center">⁂</div>
Footnotes
-
https://www.scientecheasy.com/2020/09/collection-hierarchy-in-java.html/ ↩ ↩2 ↩3 ↩4
-
https://www.geeksforgeeks.org/java/java-collection-tutorial/ ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11 ↩12 ↩13 ↩14 ↩15 ↩16
-
https://www.tutorialspoint.com/java/java_collections.htm ↩ ↩2
-
https://utho.com/blog/java-collection-framework-benefits-types-diagram/ ↩ ↩2
-
https://stackoverflow.com/questions/40484657/arraylist-linkedlist-and-vector-which-one-is-the-best-for-adding-or-removing-th ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8
-
https://dzone.com/articles/arraylist-vs-linkedlist-vs ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9
-
https://byjus.com/gate/difference-between-hashset-and-treeset-in-java/ ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8
-
https://www.scaler.com/topics/hashset-vs-treeset/ ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8
-
https://codevisionz.com/lessons/java-hashset-hashmap-treeset-treemap-guide/ ↩ ↩2 ↩3 ↩4 ↩5 ↩6
-
https://www.reddit.com/r/javahelp/comments/u6pouv/what_threadsafety_does_concurrenthashmap_provide/ ↩ ↩2
-
https://www.geeksforgeeks.org/java/listiterator-in-java/ ↩ ↩2
-
https://beginnersbook.com/2014/06/listiterator-in-java-with-examples/ ↩
-
https://www.javaguides.net/2018/07/java-util-collections-class-methods-guide.html ↩ ↩2
-
https://www.w3resource.com/java-tutorial/java-collections-utility-class.php ↩ ↩2 ↩3 ↩4 ↩5 ↩6
-
https://springframework.guru/sorting-java-collections/ ↩ ↩2 ↩3
-
https://stackoverflow.com/questions/12115727/thread-safety-with-concurrenthashmap ↩
-
https://www.w3schools.com/java/ref_arraylist_listiterator.asp ↩