Skip to main content
Java Basics
CHAPTER 20 Beginner

Collections Framework

Updated: May 17, 2026
5 min read

# CHAPTER 20

Collections Framework

1. Introduction

Arrays are fixed-size. The Collections Framework provides dynamic, resizable data structures — lists, sets, maps, and queues. It is the most used part of the Java Standard Library in enterprise applications.

2. Collections Hierarchy

123456789101112131415161718
Collection (Interface)
├── List (ordered, duplicates allowed)
│   ├── ArrayList
│   ├── LinkedList
│   └── Vector
├── Set (no duplicates)
│   ├── HashSet
│   ├── LinkedHashSet
│   └── TreeSet
└── Queue (FIFO)
    ├── PriorityQueue
    └── LinkedList

Map (Interface — separate hierarchy)
├── HashMap
├── LinkedHashMap
├── TreeMap
└── Hashtable

3. ArrayList

Dynamic array that grows automatically:
java
123456789101112131415
import java.util.ArrayList;

ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
names.get(0);           // "Alice"
names.set(1, "Bobby");  // Replace at index 1
names.remove(2);        // Remove index 2
names.size();            // 2
names.contains("Alice"); // true

for (String name : names) {
    System.out.println(name);
}

4. LinkedList

Doubly-linked list — efficient for insertions/deletions:
java
123456
import java.util.LinkedList;

LinkedList<String> queue = new LinkedList<>();
queue.addFirst("First");
queue.addLast("Last");
queue.removeFirst();

5. HashMap

Key-value pairs with O(1) average lookup:
java
123456789101112131415
import java.util.HashMap;

HashMap<String, Integer> scores = new HashMap<>();
scores.put("Alice", 95);
scores.put("Bob", 87);
scores.put("Charlie", 92);

System.out.println(scores.get("Alice")); // 95
scores.containsKey("Bob");               // true
scores.remove("Charlie");
scores.size();                            // 2

for (var entry : scores.entrySet()) {
    System.out.println(entry.getKey() + ": " + entry.getValue());
}

6. HashSet

Unique elements, no duplicates:
java
1234567
import java.util.HashSet;

HashSet<String> languages = new HashSet<>();
languages.add("Java");
languages.add("Python");
languages.add("Java"); // Ignored — duplicate!
System.out.println(languages.size()); // 2

7. Iterator

java
123456789
import java.util.Iterator;

ArrayList<String> list = new ArrayList<>();
list.add("A"); list.add("B"); list.add("C");

Iterator<String> it = list.iterator();
while (it.hasNext()) {
    System.out.println(it.next());
}

8. Collections Utility Class

java
123456789
import java.util.Collections;

ArrayList<Integer> nums = new ArrayList<>(List.of(5, 2, 8, 1, 9));
Collections.sort(nums);          // [1, 2, 5, 8, 9]
Collections.reverse(nums);       // [9, 8, 5, 2, 1]
Collections.shuffle(nums);       // Random order
Collections.max(nums);           // 9
Collections.min(nums);           // 1
Collections.frequency(nums, 5);  // Count of 5

9. Mini Project: Contact Manager

java
12345678910111213141516171819202122232425262728293031323334353637
import java.util.*;

public class ContactManager {
    public static void main(String[] args) {
        HashMap<String, String> contacts = new HashMap<>();
        Scanner sc = new Scanner(System.in);
        boolean running = true;

        while (running) {
            System.out.println("\n1. Add  2. Search  3. List All  4. Delete  5. Exit");
            System.out.print("Choice: ");
            int choice = sc.nextInt(); sc.nextLine();

            switch (choice) {
                case 1:
                    System.out.print("Name: "); String name = sc.nextLine();
                    System.out.print("Phone: "); String phone = sc.nextLine();
                    contacts.put(name, phone);
                    System.out.println("Contact added!");
                    break;
                case 2:
                    System.out.print("Search name: "); String search = sc.nextLine();
                    System.out.println(contacts.getOrDefault(search, "Not found"));
                    break;
                case 3:
                    contacts.forEach((k, v) -> System.out.println(k + ": " + v));
                    break;
                case 4:
                    System.out.print("Delete name: "); String del = sc.nextLine();
                    contacts.remove(del);
                    break;
                case 5: running = false; break;
            }
        }
        sc.close();
    }
}

10. MCQ Quiz with Answers

Question 1

ArrayList vs Array?

Question 2

HashMap stores:

Question 3

HashSet allows duplicates?

Question 4

Which has fastest lookup?

Question 5

Collections.sort() sorts:

Question 6

LinkedList is best for:

Question 7

TreeSet stores elements:

Question 8

Which interface do all collections extend?

Question 9

HashMap allows null keys?

Question 10

list.size() returns:

11. Interview Questions

  • Q: ArrayList vs LinkedList — when to use each?
  • Q: HashMap vs TreeMap vs LinkedHashMap?
  • Q: How does HashMap work internally? (Hashing, buckets)
  • Q: What is the difference between fail-fast and fail-safe iterators?

12. Summary

The Collections Framework provides dynamic, powerful data structures. ArrayList for dynamic lists, HashMap for key-value lookups, HashSet for unique elements. The Collections utility class provides sorting, searching, and manipulation methods. Master these for enterprise Java development.

13. Next Chapter Recommendation

In Chapter 21: Generics in Java, we'll learn how to write type-safe, reusable code using generic classes and methods.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·