Java Collections Visual Guide
Learn what each Java collection is for, how they differ, and when to use them in real programs. This guide helps you move beyond memorising names and toward choosing the right structure with confidence.
Why this matters
Collections are central to Java development. Many beginner mistakes come from using the wrong structure, not from syntax alone.
Best use
Use this page as a decision guide before linking readers into deeper tutorials on lists, sets, maps, queues, generics, streams, and algorithmic thinking.
Strong content value
This is more than an article. It functions as a reusable reference page that learners can return to repeatedly.
Quick decision guide
List
Set
Map
Queue or Deque
ArrayList
A resizable array that stores elements in order and allows duplicates.
Best when
- You need fast access by index
- You want ordered data
- You are frequently reading more than inserting in the middle
Example use case
Storing a list of course lessons, products, or user comments in display order.
List<String> lessons = new ArrayList<>();
lessons.add("Variables");
lessons.add("Loops");
lessons.add("Methods");
LinkedList
A linked structure that also preserves order and allows duplicates.
Best when
- You frequently add or remove at the beginning or middle
- You do not rely heavily on random index access
- You also want queue-like operations
Example use case
Simple queue-like processing or frequent insertion/removal workflows.
List<String> tasks = new LinkedList<>();
tasks.add("Read");
tasks.add("Code");
tasks.remove(0);
HashSet
A set that stores unique elements with no guaranteed order.
Best when
- You want to remove duplicates
- You do not care about insertion order
- You need fast membership checks
Example use case
Tracking unique usernames, visited pages, or completed identifiers.
Set<String> tags = new HashSet<>();
tags.add("java");
tags.add("backend");
tags.add("java");
LinkedHashSet
A set that preserves insertion order while still preventing duplicates.
Best when
- You want uniqueness and stable display order
- You care about the order items were added
- You want cleaner output than a plain HashSet
Example use case
Displaying unique categories or recently added unique items in their original sequence.
Set<String> modules = new LinkedHashSet<>();
modules.add("Core Java");
modules.add("Spring");
modules.add("Core Java");
HashMap
A key–value collection designed for fast lookup by key, with no guaranteed ordering.
Best when
- You need to associate one value with one key
- You want quick lookup
- You do not care about sorted output
Example use case
Mapping student IDs to names, usernames to profiles, or product codes to prices.
Map<Integer, String> students = new HashMap<>();
students.put(101, "Alice");
students.put(102, "Bob");
TreeMap
A key–value map that keeps keys sorted automatically.
Best when
- You need sorted keys
- You want natural ordering or comparator-based ordering
- You are willing to trade some speed for sorted structure
Example use case
Displaying scores, dates, or alphabetically sorted labels in a predictable order.
Map<String, Integer> scores = new TreeMap<>();
scores.put("Charlie", 88);
scores.put("Alice", 95);
Queue
A first-in, first-out structure useful for processing tasks in arrival order.
Best when
- You want FIFO behaviour
- You are processing jobs, messages, or requests in sequence
- You need poll/offer semantics rather than index-based access
Example use case
Request handling, printing tasks, and turn-based processing systems.
Queue<String> queue = new LinkedList<>();
queue.offer("Task 1");
queue.offer("Task 2");
queue.poll();
Deque as Modern Stack
For last-in, first-out behaviour, Deque is generally preferred over the older Stack class.
Best when
- You need push/pop behaviour
- You want modern stack usage
- You may later want double-ended operations
Example use case
Undo operations, expression parsing, navigation history, or bracket checking.
Deque<String> stack = new ArrayDeque<>();
stack.push("Page A");
stack.push("Page B");
stack.pop();
Quick comparison table
| Structure | Ordering | Duplicates | Main strength |
|---|---|---|---|
| ArrayList | Yes | Yes | Fast index access |
| LinkedList | Yes | Yes | Flexible insertion/removal |
| HashSet | No guaranteed order | No | Fast uniqueness checks |
| LinkedHashSet | Insertion order | No | Unique plus stable order |
| HashMap | No guaranteed order | Keys unique | Fast lookup by key |
| TreeMap | Sorted keys | Keys unique | Ordered key access |
| Queue | FIFO | Depends | Task processing order |
| Deque | Both ends | Depends | Queue and stack flexibility |