100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

Collections Framework in Java

Understand the Java Collections Framework hierarchy — Collection vs Map, List/Set/Queue interfaces, and how to pick the right implementation.

Collections FrameworkBeginner9 min readJul 7, 2026
Analogies

1. Introduction

The Java Collections Framework (JCF) is a unified architecture for storing and manipulating groups of objects. It provides ready-made interfaces (List, Set, Queue, Map) and implementations (ArrayList, HashSet, HashMap, etc.) so you don't have to write your own data structures from scratch.

🏏

Cricket analogy: The ICC's standardized fielding positions (like slip, gully, mid-on) are ready-made roles a captain can assign instead of inventing new fielding names for every match, just as the Java Collections Framework offers ready-made List, Set, and Map structures.

Before collections, Java only had arrays (fixed size) and legacy classes like Vector and Hashtable. The JCF, introduced in Java 1.2, standardized how data is stored, traversed, and sorted, and made every implementation interchangeable through common interfaces.

🏏

Cricket analogy: Before helmets became standard equipment in the 1970s, batsmen used makeshift caps and improvised protection; the eventual standardized helmet rule made every player's gear interchangeable and safer, just as Java 1.2 standardized collections after ad-hoc arrays and Vector.

2. Syntax

text
// Collection hierarchy (interfaces)
Collection<E>  --> List<E>, Set<E>, Queue<E>
Map<K, V>      --> a SEPARATE hierarchy, NOT a subtype of Collection

// Common declarations
List<String> list = new ArrayList<>();
Set<String> set = new HashSet<>();
Map<String, Integer> map = new HashMap<>();
Queue<Integer> queue = new LinkedList<>();

3. Explanation

At the top of the hierarchy sits the Collection interface, which is extended by List (ordered, allows duplicates), Set (no duplicates), and Queue (FIFO-style processing). Map is deliberately excluded from the Collection hierarchy because it stores key-value pairs rather than single elements — this is one of the most common exam and interview traps.

🏏

Cricket analogy: A cricket squad list allows duplicate skill sets (multiple all-rounders) like List, while the playing XI's unique batting order has no repeated players like Set; but the scoreboard, which pairs each player's name with runs like a Map, is a separate ledger, not part of the squad hierarchy.

Each interface has multiple implementations with different performance trade-offs: ArrayList vs LinkedList for List, HashSet vs TreeSet vs LinkedHashSet for Set, and HashMap vs TreeMap vs LinkedHashMap for Map. Choosing the right one depends on whether you need ordering, sorting, or fast random access.

🏏

Cricket analogy: Choosing between a fast opening batsman like Rohit Sharma (ArrayList, quick random access) and a patient anchor like Cheteshwar Pujara (LinkedList, efficient insertions) depends on match situation, just as picking ArrayList versus LinkedList depends on your access pattern.

Exam trap: Map does NOT extend Collection. If asked 'which of these interfaces extends Collection', Map is always the wrong answer alongside List, Set, and Queue.

4. Example

java
import java.util.*;

public class CollectionsDemo {
    public static void main(String[] args) {
        // List: ordered, duplicates allowed
        List<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Apple");

        // Set: no duplicates
        Set<String> uniqueFruits = new HashSet<>(fruits);

        // Map: key-value pairs, not a Collection
        Map<String, Integer> stock = new HashMap<>();
        stock.put("Apple", 50);
        stock.put("Banana", 30);

        System.out.println("List: " + fruits);
        System.out.println("Set size: " + uniqueFruits.size());
        System.out.println("Map: " + stock);
    }
}

5. Output

text
List: [Apple, Banana, Apple]
Set size: 2
Map: {Apple=50, Banana=30}

6. Key Takeaways

  • Collection is the root interface for List, Set, and Queue.
  • Map is a separate hierarchy and does NOT extend Collection.
  • List preserves insertion order and allows duplicates; Set does not allow duplicates.
  • Choosing an implementation (ArrayList vs LinkedList, HashMap vs TreeMap) depends on required ordering and performance.
  • The framework was introduced in Java 1.2 to replace ad-hoc structures like Vector and Hashtable.

Practice what you learned

Was this page helpful?

Topics covered

#Java#JavaProgrammingStudyNotes#Programming#CollectionsFrameworkInJava#Collections#Framework#Syntax#Explanation#StudyNotes#SkillVeris