Skip to content

πŸ“š Java Collections Framework – Master Lists, Sets, Maps, and Queues

πŸ”– Introduction

When building real-world applications, we often need to store, retrieve, and manipulate groups of objects. Java provides a rich and powerful Collections Framework to make this easy.

In this lesson, we’ll explore:

  • List (ArrayList, LinkedList)
  • Set (HashSet, TreeSet)
  • Map (HashMap, TreeMap)
  • Queue (PriorityQueue, LinkedList)

🧱 What is the Java Collections Framework?

A Collection is a container object that holds a group of elements.

The Collections Framework provides:

  • Interfaces (List, Set, Map, Queue)
  • Implementations (ArrayList, HashMap, etc.)
  • Utility classes (Collections, Arrays)

πŸ“‹ List – Ordered Collection (Duplicates Allowed)

πŸ”Ή ArrayList

import java.util.ArrayList;

public class ListExample {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Apple");

        for (String fruit : fruits) {
            System.out.println(fruit);
        }
    }
}

βœ… Preserves insertion order
βœ… Allows duplicates
βœ… Dynamic resizing

πŸ”Ή LinkedList

Similar to ArrayList, but optimized for frequent insertions/deletions.

LinkedList<String> items = new LinkedList<>();
items.add("One");
items.addFirst("Zero");
items.addLast("Two");

🚫 Set – No Duplicates Allowed

πŸ”Ή HashSet

import java.util.HashSet;

public class SetExample {
    public static void main(String[] args) {
        HashSet<String> cities = new HashSet<>();
        cities.add("Delhi");
        cities.add("Mumbai");
        cities.add("Delhi");

        System.out.println(cities);  // Duplicates ignored
    }
}

βœ… No duplicates
❌ Unordered (no guarantee of insertion order)


πŸ”Ή TreeSet – Sorted Set

import java.util.TreeSet;

TreeSet<Integer> numbers = new TreeSet<>();
numbers.add(10);
numbers.add(5);
numbers.add(15);

System.out.println(numbers);  // Output: [5, 10, 15]

πŸ—ΊοΈ Map – Key-Value Pairs

πŸ”Ή HashMap

import java.util.HashMap;

public class MapExample {
    public static void main(String[] args) {
        HashMap<String, Integer> ages = new HashMap<>();
        ages.put("Alice", 25);
        ages.put("Bob", 30);

        System.out.println(ages.get("Alice"));  // Output: 25
    }
}

βœ… Fast lookup
βœ… Keys must be unique
βœ… Values can be duplicated


πŸ”Ή TreeMap – Sorted by Keys

import java.util.TreeMap;

TreeMap<String, Integer> scores = new TreeMap<>();
scores.put("Math", 90);
scores.put("English", 85);

System.out.println(scores);  // Sorted by keys

πŸ›€οΈ Queue – FIFO (First In First Out)

πŸ”Ή PriorityQueue

import java.util.PriorityQueue;

PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.add(20);
pq.add(10);
pq.add(30);

while (!pq.isEmpty()) {
    System.out.println(pq.poll());  // Sorted order: 10, 20, 30
}

βœ… Prioritizes elements (natural order by default)


πŸ”§ Utility Class: Collections

import java.util.Collections;

ArrayList<String> names = new ArrayList<>();
names.add("John");
names.add("Alice");
Collections.sort(names);
System.out.println(names);  // [Alice, John]

🧠 Quick Summary

InterfaceAllows DuplicatesMaintains OrderSortedKey-Value
Listβœ… Yesβœ… Yes❌ No❌ No
Set❌ No❌ No (HashSet)βœ… (TreeSet)❌ No
Map❌ Keys❌ Noβœ… (TreeMap)βœ… Yes
Queueβœ… Yesβœ… FIFO / Priorityβœ… (PriorityQueue)❌ No

πŸ§ͺ Mini Challenge

  • Create a HashMap<String, Integer> to store the names and scores of 3 students.
  • Use a TreeSet<Integer> to keep their scores sorted.

βœ… What You Learned

  • Difference between List, Set, Map, and Queue
  • When to use ArrayList, HashSet, HashMap, etc.
  • How to choose the right data structure based on your needs

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *