Stop 08 · Collections & GenericsTopics · 1/10

Collections Framework Overview

Part of the Java Roadmap

In short

Almost every real Java program holds groups of things: orders, users, cities, tokens. The Collections Framework gives you ready-made, well-tested containers for all of them, so you never have to build your own resizable array again.

The Collections Framework provides interfaces (List, Set, Queue, Map) and implementations (ArrayList, HashSet, HashMap and more). Code against the interface, like List<String> list = new ArrayList<>().

The jaadu stands in a bright kitchen sorting spices into a neat rack, a jar, a queue of cups and a row of labelled steel dabbas.

Think of it like this

A well-organised kitchen

A kitchen has a spice rack where order matters (List), a jar of unique masalas with no repeats (Set), a token line at the chai counter where the first in is served first (Queue), and labelled dabbas where each label points to one item (Map). Same kitchen, different containers for different jobs.

Watch it happen

The main family tree in java.util

java.util
Collection
Map
List
Set
Queue
ArrayList
HashSet
ArrayDeque
HashMap
TreeMap

1/4Everything lives in the java.util package.

The big ideas

What you need to know

List keeps order

Elements stay in insertion order, can repeat and are reachable by index. ArrayList is the usual choice.

Set keeps unique items

Adding a duplicate is silently ignored. Great for unique visitors, tags or roll numbers.

Map links keys to values

Look up a value by its key, like a PIN code by city name. Keys are unique, values can repeat.

Code to the interface

Write List<String> names = new ArrayList<>(). You can later swap the implementation without touching the rest of the code.

See it in code

Walk through the code

java
1import java.util.*;2 3public class CollectionsTour {4    public static void main(String[] args) {5        List<String> cities = new ArrayList<>();6        cities.add("Pune");7        cities.add("Jaipur");8        cities.add("Pune");9 10        Set<String> unique = new HashSet<>(cities);11 12        Map<String, Integer> pinCodes = new HashMap<>();13        pinCodes.put("Pune", 411001);14        pinCodes.put("Jaipur", 302001);15 16        Queue<String> tokens = new ArrayDeque<>();17        tokens.offer("T-101");18        tokens.offer("T-102");19 20        System.out.println(cities);               // [Pune, Jaipur, Pune]21        System.out.println(unique.size());        // 222        System.out.println(pinCodes.get("Pune")); // 41100123        System.out.println(tokens.poll());        // T-10124    }25}

1/4A List keeps both copies of Pune, in the order you added them.

One quick tour of the four main container types. Notice every variable is declared with the interface type.

Save yourself the pain

Common mistakes

Oops

Declaring ArrayList<String> list = new ArrayList<>() everywhere, including method parameters.

Do this instead

Use the interface type, List<String>. Methods that accept List work with any list implementation.

Oops

Using a List and calling contains() in a loop to avoid duplicates.

Do this instead

Use a Set. It is built for uniqueness and checks membership much faster.

Oops

Trying to store primitives like List<int>.

Do this instead

Collections hold objects, so write List<Integer>. Autoboxing converts int values for you.

Your mission

Pick the right container

~20 min

Quick check

Which of these does NOT extend the Collection interface?

Go deeper

  1. 1Draw the hierarchy of Collection, List, Set, Queue and the separate Map interface.
  2. 2Declare variables with interface types instead of implementation types.
  3. 3Use List.of() and Map.of() for small, immutable collections.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge