Java Lesson 32 – List,Set,Map | Dataplexa

List, Set, and Map

The Java Collections Framework provides different interfaces to store and manage data. Among them, List, Set, and Map are the most important and widely used.

Choosing the right collection type is critical in real-world Java applications. Each one serves a different purpose and behaves differently.


Why Java Provides Different Collection Types

Not all data behaves the same way. Sometimes order matters, sometimes uniqueness matters, and sometimes fast lookup using keys is required.

Java provides List, Set, and Map so developers can model data exactly the way real applications need it.


List – Ordered Collection

A List stores elements in an ordered sequence and allows duplicate values.

Use a List when:

  • Order matters
  • Duplicate values are allowed
  • Index-based access is required

List Example


import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {

        List tasks = new ArrayList<>();

        tasks.add("Wake up");
        tasks.add("Work");
        tasks.add("Exercise");
        tasks.add("Work");

        System.out.println(tasks);
        System.out.println("First task: " + tasks.get(0));
    }
}

Lists are commonly used for to-do lists, logs, product lists, and ordered records.


Set – Unique Collection

A Set stores unique elements only. Duplicate values are automatically ignored.

Use a Set when:

  • Uniqueness is required
  • Order is not important
  • Fast existence checking is needed

Set Example


import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) {

        Set usernames = new HashSet<>();

        usernames.add("admin");
        usernames.add("guest");
        usernames.add("admin");

        System.out.println(usernames);
    }
}

Sets are often used for unique IDs, usernames, email lists, and permission sets.


Map – Key-Value Collection

A Map stores data as key-value pairs. Each key is unique, but values can be duplicated.

Use a Map when:

  • Data is identified by a key
  • Fast lookup is required
  • Relationships between key and value exist

Map Example


import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {

        Map employees = new HashMap<>();

        employees.put(101, "Alice");
        employees.put(102, "Bob");

        System.out.println(employees);
        System.out.println("Employee 101: " + employees.get(101));
    }
}

Maps are widely used for database results, configuration settings, caching, and lookups.


Quick Comparison

  • List – ordered, duplicates allowed
  • Set – unordered, no duplicates
  • Map – key-value pairs

Understanding this difference helps avoid performance and logic issues.


Real-World Usage Scenarios

  • List – shopping cart items
  • Set – registered user emails
  • Map – user ID to user profile

Each collection type models data differently based on real needs.


Key Takeaways

  • List, Set, and Map serve different purposes
  • Choosing the right one is critical
  • They improve performance and code clarity
  • They are used everywhere in Java applications

In the next lesson, we will dive deeper into ArrayList and understand how it works internally.