Backend Notes DEC 10, 2025 • 03 MIN READ

LIST VS
ARRAYLIST VS
HASHMAP VS HASHSET

Abstract technical background

If you’ve been writing Java long enough, you’ll hit this classic confusion: “What’s the difference between List, ArrayList, HashMap, and HashSet?”

I remember early in my career, I mixed these up all the time. I threw everything into an ArrayList, used HashMap when I didn’t know what else to use, and ignored HashSet because I didn’t understand why it mattered.

Let’s break them down cleanly — the way I wish someone had explained them to me back then.

List — The Interface (The Contract)

Think of List as the rules or agreement. It doesn’t care how things are stored. It just promises:

  • check_circle You can add items
  • check_circle You can remove items
  • check_circle You can access items by index
  • check_circle Order is maintained

But List itself doesn’t implement anything. It’s just the blueprint.

Example:

ListExample.java JAVA
List<String> names = new ArrayList<>();
names.add("Joekawai");
names.add("Fufufafa");
names.add("Sahorni");

Here, you code against the interface (List) but use a concrete class (ArrayList) underneath. Why? Because this makes your code flexible if you want to switch to LinkedList later.

ArrayList — The Common Implementation of List

ArrayList is the everyday workhorse. It stores elements inside a resizable array.

key behavior:

  • check_circle Maintains order
  • check_circle Allows duplicates
  • check_circle Fast random access (get by index)
  • check_circle Slower for insert/remove at the beginning

If you just need a “normal list”, ArrayList is almost always the default.

Example:

ArrayListExample.java JAVA
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(20); // duplicates allowed
System.out.println(numbers); // [10, 20, 20]

Good when you care about order and need fast access.

HashMap — Key–Value Storage

HashMap is your dictionary. You don’t store individual items — you store pairs:

key → value

Keys must be unique. Values can be anything (including duplicates). Perfect when you want to find data by a label, not by index.
Example:

HashMapExample.java JAVA
HashMap<String, Integer> ages = new HashMap<>();
ages.put("Joekawai", 25);
ages.put("Fufufafa", 30);
ages.put("Joekawai", 26); // overwrites previous value

System.out.println(ages.get("Joekawai")); // 26

Useful when you map something like id → object or username → profile.

HashSet — Unique Values Only

HashSet is like a box where duplicates aren’t allowed. If you add something that already exists, it ignores it.
No indices, no ordering guarantee — just uniqueness.

Example:

HashSetExample.java JAVA
HashSet<String> fruits = new HashSet<>();
fruits.add("Joekawai");
fruits.add("Fufufafa");
fruits.add("Joekawai"); // ignored

System.out.println(fruits); // [Joekawai, Fufufafa]

Use it when you want to guarantee no duplicates.

When — to Use What?

After years of building apps, here’s the practical way I think about them:

  • check_circle List → When you want flexibility (always code to interfaces).
  • check_circle ArrayList → When you need an ordered, index-based list.
  • check_circle HashMap → When your data has meaningful keys.
  • check_circle HashSet → When you just want uniqueness and nothing else.

If you keep this mental model, you’ll stop mixing them up.

Quick Combined Example

AllExample.java JAVA
List<String> participants = new ArrayList<>();
participants.add("Joekawai");
participants.add("Joekawai"); // duplicates allowed

HashSet<String> uniqueParticipants = new HashSet<>(participants);
// now duplicates are removed

HashMap<String, Integer> scores = new HashMap<>();
scores.put("Joekawai", 90);
scores.put("Fufufafa", 85);

System.out.println("List: " + participants);
System.out.println("Set: " + uniqueParticipants);
System.out.println("Map: " + scores);

Output:

Output.txt LOG
List: [Joekawai, Joekawai]
Set: [Joekawai]
Map: {Joekawai=90, Fufufafa=85}

Final Thought

Once you stop treating List, ArrayList, HashMap, and HashSet like siblings and start seeing them as different tools for different jobs, Java collection APIs finally start making sense.

READ BEYOND THE VOID

Technical blueprint background
Backend Notes

How ForkJoinPool Works Internally

How ForkJoinPool working...

Read Entry
Abstract digital network
Backend Notes

How proxy and constructor injection works in Spring Boot

How proxy and constructor injection in Springboot...

Read Entry
Circuit board macro
Backend Notes

How the JVM Handles Class Loading and Garbage Collection

How garbage collection work in Springboot...

Read Entry