About Us Contact Us Write for Us Advertise
Home > Java > List, Set and Map in Java: How to Use Each (with Examples)
Java

List, Set and Map in Java: How to Use Each (with Examples)

A hands-on guide to Java List, Set and Map — ArrayList, HashSet, and HashMap with real examples, and a simple decision guide for which one to use when.

Shiv Pandey
Shiv Pandey
Sep 23, 2026 | 61 views
List, Set and Map in Java: How to Use Each (with Examples)

In the last lesson you met the three big collection types. Now let's actually use them. This is a hands-on lesson: by the end you'll know how to work with a List, a Set, and a Map, and — most importantly — when to reach for each one. These three cover the vast majority of real-world data-handling in Java.

List — an ordered sequence (duplicates allowed)

Use a List when order matters and you might have repeats. ArrayList is the go-to implementation:

import java.util.*;

List<String> tasks = new ArrayList<>();
tasks.add("Wake up");
tasks.add("Code");
tasks.add("Code");        // duplicates are fine in a List

System.out.println(tasks.get(1));    // "Code" — access by index
System.out.println(tasks.size());    // 3
tasks.remove("Wake up");
System.out.println(tasks);           // [Code, Code]

Notice we declared the variable as List<String> but created an ArrayList. That's good practice — "program to the interface" — and it comes straight from the abstraction ideas in Lesson 17.

Set — unique items only (no duplicates)

Use a Set when you want to guarantee no duplicates — like a collection of unique tags or email addresses. Add a duplicate and it's simply ignored:

Set<String> tags = new HashSet<>();
tags.add("java");
tags.add("coding");
tags.add("java");        // ignored — already present

System.out.println(tags.size());        // 2, not 3
System.out.println(tags.contains("java"));   // true — very fast lookup

One thing to know: a HashSet does not keep insertion order. If you need the items sorted, use a TreeSet; if you need to preserve insertion order, use a LinkedHashSet.

Map — key → value pairs (a dictionary)

A Map stores pairs: each unique key maps to a value. It's perfect for lookups — "given this key, get me that value." HashMap is the workhorse:

Map<String, Integer> ages = new HashMap<>();
ages.put("Asha", 25);       // key "Asha" → value 25
ages.put("Ravi", 30);

System.out.println(ages.get("Asha"));       // 25 — instant lookup by key
System.out.println(ages.containsKey("Ravi"));  // true

Looping a Map is slightly different — you go through its entries:

for (Map.Entry<String, Integer> entry : ages.entrySet()) {
    System.out.println(entry.getKey() + " is " + entry.getValue());
}
// Asha is 25
// Ravi is 30

The decision guide: which one do I use?

If you need… Use Example
An ordered list, duplicates OK ArrayList A to-do list, blog posts
Only unique items HashSet Unique tags, visited IDs
Lookup by a key HashMap Username → user, settings

When you're unsure, ask yourself: Do I need order and duplicates? → List. Do I need uniqueness? → Set. Am I pairing keys to values? → Map. That single question answers it almost every time.

These three are genuinely the collections I use most in real work, and I'd bet most Java developers would say the same. On this website, blog posts come back as a List, app settings are looked up from a Map by their key, and unique things like tags live in a Set. Once picking the right one becomes second nature, a whole class of clumsy, slow code just disappears from your programs.

Key takeaways

  • List (ArrayList): ordered, allows duplicates, access by index.
  • Set (HashSet): unique items only, fast contains checks, no index.
  • Map (HashMap): key → value pairs, fast lookup by key with get.
  • Choose by asking: need order/duplicates → List; uniqueness → Set; key→value → Map.

← Previous: Lesson 21 — The Collections Framework
Next: Lesson 23 — Generics in Java →

Related Articles

The Java Collections Framework: List, Set and Map Overview
Java

The Java Collections Framework: List, Set and Map Overview

Java Loops Explained: for, while and do-while (with Examples)
Java

Java Loops Explained: for, while and do-while (with Examples)

Operators in Java Explained (with Examples)
Java

Operators in Java Explained (with Examples)

How to Install Java and Set Up Your First Program (Step by Step)
Java

How to Install Java and Set Up Your First Program (Step by Step)