Back to blog
January 3, 20264 min readMarina

List, Set, and Map: Key Differences in Java Collections

JavaCollections

List, Set, and Map: Key Differences in Java Collections

List, Set, and Map Overview

When working with Java, understanding the differences between List, Set, and Map is essential.

List

  • Ordered collection, allows duplicates
  • Access by index
  • Common implementations: ArrayList, LinkedList
  • Real-world example: storing a user's transaction history
  • Use case: showing transactions in the exact order they occurred
  • List<Transaction> transactions = new ArrayList<>();
    transactions.add(new Transaction("Deposit", 100));
    transactions.add(new Transaction("Withdrawal", 50));
    transactions.add(new Transaction("Deposit", 200)); // duplicates allowed

    Set

  • Collection of unique elements (no duplicates)
  • Order depends on implementation (HashSet, LinkedHashSet, TreeSet)
  • Real-world example: each account number must be unique
  • Use case: preventing duplicate account entries in the system
  • Set<String> set = new HashSet<>();
    set.add("apple");
    set.add("banana");
    set.add("apple"); // ignored

    Map

  • Stores key-value pairs
  • Keys must be unique, values can repeat
  • Common implementations: HashMap, LinkedHashMap, TreeMap
  • Real-world example: account number as the key and balance as the value
  • Use case: quickly looking up the balance for a specific account
  • Map<String, Integer> map = new HashMap<>();
    map.put("apple", 3);
    map.put("banana", 5);
    map.put("apple", 10); // overwrites previous

    Think

  • List → array with duplicates
  • Set → bag of unique elements
  • Map → dictionary or phonebook
  • If you want to see how these structures show up in real systems, especially in finance, check out

    Java Collections in Real Systems | Part 1: Set.