Java Collections Framework: List, Set, Map, and Queue Explained
Learn how Java collections work with practical examples of List, Set, Map, and Queue, including when to use each collection and how to choose the right data structure.

Tools used: Java JDK, IntelliJ IDEA, VS Code, Maven
Prerequisites: Basic Java syntax, classes, objects, variables, loops, and methods.
Java Collections Framework: List, Set, Map, and Queue Explained
When you write Java applications, you rarely work with just one value.
You usually need to store:
- A list of users
- A set of unique permissions
- A mapping between user IDs and users
- A queue of background tasks
- A collection of API results
- A group of database records
Java provides the Collections Framework to make these operations easier and more consistent.
The challenge for beginners is not learning the names of collections.
It is knowing which collection to choose for a particular problem.
A simple way to think about Java collections is: List = ordered values, Set = unique values, Map = key-value relationships, Queue = work waiting to be processed.
Quick Answer
| Collection | Main Purpose | Allows Duplicates? | Example |
|---|---|---|---|
ArrayList | Ordered data | Yes | User names |
LinkedList | Sequential data | Yes | Queue-like operations |
HashSet | Unique values | No | Unique skills |
TreeSet | Sorted unique values | No | Sorted scores |
HashMap | Key-value lookup | Keys: No | User ID → User |
TreeMap | Sorted key-value data | Keys: No | Sorted IDs |
PriorityQueue | Priority-based processing | Yes | Jobs by priority |
What Is the Java Collections Framework?
The Java Collections Framework provides interfaces and implementations for storing and manipulating groups of objects.
Instead of creating your own data structure for every application, you can use standard interfaces and implementations.
A simplified relationship looks like this:
The important distinction is that Map is not a subtype of Collection. It represents key-value relationships separately.
1. List — When Order Matters
A List stores elements in a sequence.
It allows duplicate values and provides index-based access.
Example:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("Python");
languages.add("Java");
System.out.println(languages);
}
}Output:
[Java, Python, Java]The duplicate "Java" is allowed.
ArrayList
ArrayList is one of the most commonly used Java collections.
List<String> developers = new ArrayList<>();
developers.add("Amit");
developers.add("Rahul");
developers.add("Priya");
System.out.println(developers.get(0));Output:
AmitBecause a list is ordered, you can access elements by index.
developers.get(1);returns the second element.
When Should You Use ArrayList?
Use ArrayList when you generally need:
- Ordered data
- Fast access by index
- Duplicate values
- Frequent reading
- A general-purpose list
For many everyday application scenarios, ArrayList is a good default choice.
ArrayList Example: API Results
Imagine a backend application receives users from an API.
List<String> users = new ArrayList<>();
users.add("Sovit");
users.add("Ankit");
users.add("Priya");
for (String user : users) {
System.out.println(user);
}This is useful for situations such as:
API Response
↓
Java Objects
↓
List<User>
↓
Business Logic
↓
Response2. LinkedList
LinkedList implements the List and Deque interfaces.
A simple example:
LinkedList<String> tasks = new LinkedList<>();
tasks.add("Build");
tasks.add("Test");
tasks.add("Deploy");
System.out.println(tasks);You can also work with the beginning and end of the sequence:
tasks.addFirst("Plan");
tasks.addLast("Monitor");However, you should not automatically choose LinkedList just because you heard that inserting elements can be efficient.
For many normal application workloads, ArrayList is the simpler and more appropriate choice.
ArrayList vs LinkedList
| Feature | ArrayList | LinkedList |
|---|---|---|
| Random access | Excellent | Slower |
| Memory overhead | Lower | Higher |
| General-purpose use | Excellent | Less common |
| Queue/deque operations | Not its main purpose | Useful |
| Index-based access | Good | Poorer |
The right choice depends on the operations your application performs most frequently.
3. Set — When Values Must Be Unique
A Set is useful when duplicate values should not be stored.
Example:
Set<String> skills = new HashSet<>();
skills.add("Java");
skills.add("Python");
skills.add("Java");
System.out.println(skills);The second "Java" does not create another entry.
This makes a Set useful for:
- Unique skills
- Unique tags
- Permission names
- IDs that must not repeat
- Deduplicating data
HashSet
HashSet is a common implementation of Set.
Set<Integer> numbers = new HashSet<>();
numbers.add(10);
numbers.add(20);
numbers.add(10);
System.out.println(numbers);The set contains only one 10.
Do not rely on HashSet for a meaningful iteration order.
If ordering matters, choose a collection designed for that requirement.
TreeSet
TreeSet stores unique values while maintaining sorted order.
Set<Integer> scores = new TreeSet<>();
scores.add(80);
scores.add(40);
scores.add(90);
scores.add(70);
System.out.println(scores);Output:
[40, 70, 80, 90]This can be useful when you need uniqueness and sorted traversal.
4. Map — Key-Value Relationships
A Map is different from List and Set.
It stores data as:
Key → ValueFor example:
1001 → User
1002 → User
1003 → UserA common implementation is HashMap.
Map<Integer, String> users = new HashMap<>();
users.put(101, "Amit");
users.put(102, "Priya");
users.put(103, "Rahul");
System.out.println(users.get(102));Output:
PriyaThis is extremely useful when you need to find a value using a known key.
HashMap in a Real Application
Imagine your backend receives a user ID:
/api/users/102Instead of searching through every user, you might have data represented conceptually as:
Map<Integer, User> usersById;Then:
User user = usersById.get(102);The map represents:
User ID
↓
User ObjectThis pattern appears frequently in backend applications.
HashMap Example
Map<String, Integer> stock = new HashMap<>();
stock.put("Laptop", 10);
stock.put("Keyboard", 25);
stock.put("Mouse", 40);
System.out.println(stock.get("Keyboard"));Output:
25You can also check whether a key exists:
if (stock.containsKey("Laptop")) {
System.out.println("Laptop exists");
}TreeMap
TreeMap keeps keys sorted.
Map<Integer, String> employees = new TreeMap<>();
employees.put(103, "Priya");
employees.put(101, "Amit");
employees.put(102, "Rahul");
System.out.println(employees);The keys are maintained in sorted order.
Use it when sorted key traversal is part of the requirement.
5. Queue — Process Items in Order
A queue represents items waiting to be processed.
A simple mental model is:
First In
↓
[Task 1]
[Task 2]
[Task 3]
↓
First OutExample:
Queue<String> tasks = new LinkedList<>();
tasks.add("Build");
tasks.add("Test");
tasks.add("Deploy");
System.out.println(tasks.poll());Output:
BuildThe first task was removed first.
PriorityQueue
A PriorityQueue processes elements according to priority or ordering rules rather than simple insertion order.
Example:
PriorityQueue<Integer> numbers = new PriorityQueue<>();
numbers.add(30);
numbers.add(10);
numbers.add(20);
System.out.println(numbers.poll());Output:
10This makes priority queues useful for problems such as:
- Scheduling
- Priority-based processing
- Task management
- Algorithmic problems
List vs Set vs Map vs Queue
This is the comparison you should remember.
| Requirement | Recommended Collection |
|---|---|
| Keep ordered values | List |
| Allow duplicates | List |
| Store unique values | Set |
| Find value using a key | Map |
| Process waiting items | Queue |
| Maintain sorted unique values | TreeSet |
| Maintain sorted keys | TreeMap |
| Priority-based processing | PriorityQueue |
Choosing the Right Collection
When designing Java code, ask one question first:
What operation does my application need most?
For example:
Need index-based access?
Use:
ListNeed uniqueness?
Use:
SetNeed key-based lookup?
Use:
MapNeed items processed in sequence?
Use:
QueueNeed sorted values?
Consider:
TreeSetNeed sorted keys?
Consider:
TreeMapA Practical Developer Example
Imagine you are building a job portal.
You might represent different data using different collections:
List<Job> latestJobs;
Set<String> skills;
Map<Long, Job> jobsById;
Queue<Job> jobsToProcess;Each collection solves a different problem.
This is why choosing the collection based on the application's behavior is more important than memorizing class names.
Generics Make Collections Safer
Java collections commonly use generics.
Instead of:
List users;prefer:
List<String> users;Now Java knows that the list should contain strings.
For example:
List<String> users = new ArrayList<>();
users.add("Amit");
users.add("Priya");This would cause a compile-time problem:
users.add(100);because 100 is an integer rather than a string.
Generics help catch these mistakes before the application runs.
Looping Through Collections
List
for (String language : languages) {
System.out.println(language);
}Set
for (String skill : skills) {
System.out.println(skill);
}Map
for (Map.Entry<Integer, String> entry : users.entrySet()) {
System.out.println(
entry.getKey() + " -> " + entry.getValue()
);
}Queue
while (!tasks.isEmpty()) {
String task = tasks.poll();
System.out.println(task);
}Common Mistakes
Using a List When You Need Uniqueness
If duplicates are invalid:
List<String> skills;may not communicate the requirement clearly.
A Set may be more appropriate.
Using a List for Key-Based Lookup
If you constantly search for an object by ID:
List<User> users;may require repeated searching.
A map can represent the relationship more directly:
Map<Long, User> usersById;Assuming HashSet Is Sorted
This is incorrect:
HashSet<Integer>does not mean sorted values.
If sorted order is required, consider:
TreeSet<Integer>Assuming HashMap Preserves the Order You Need
If your application requires a specific ordering rule, choose a collection that explicitly provides that behavior.
Do not build important application logic around accidental ordering.
Performance Thinking
You do not need to memorize every Big-O value to start using collections effectively.
Instead, understand the trade-off:
Data Structure
↓
Operation
↓
Performance
↓
Memory
↓
Application RequirementFor example:
ArrayListis strong for index-based access.HashMapis designed for efficient key-based lookup.HashSetis useful for uniqueness.TreeMapandTreeSetprovide sorted structures with additional ordering costs.
The correct collection depends on what your application actually needs.
Practical Mini Project
Build a small Java Job Tracker.
Store:
List<String> applications;
Set<String> skills;
Map<Integer, String> companies;
Queue<String> interviewTasks;Example:
List<String> applications = new ArrayList<>();
applications.add("Software Engineer");
applications.add("Backend Developer");
Set<String> skills = new HashSet<>();
skills.add("Java");
skills.add("SQL");
skills.add("Java");
Map<Integer, String> companies = new HashMap<>();
companies.put(101, "Company A");
companies.put(102, "Company B");
Queue<String> interviewTasks = new LinkedList<>();
interviewTasks.add("DSA");
interviewTasks.add("Java");
interviewTasks.add("System Design");This small project gives you a practical reason to use all four major collection types.
Java Collections Cheat Sheet
List
├── ArrayList
└── LinkedList
Set
├── HashSet
└── TreeSet
Map
├── HashMap
└── TreeMap
Queue
├── LinkedList
└── PriorityQueueRemember:
List → Ordered / duplicates
Set → Unique values
Map → Key → Value
Queue → Processing orderWhat This Means for Developers
Collections are not just Java syntax.
They affect how your application stores, retrieves, processes, and organizes data.
A developer working on a backend service might use:
List<Order> orders;for an ordered collection of orders.
Set<String> permissions;for unique permissions.
Map<Long, User> users;for user lookup by ID.
Queue<Job> jobs;for work waiting to be processed.
The collection communicates the intent of the code.
That makes the application easier to understand and maintain.
Interview Questions
1. What is the difference between List and Set?
A List allows duplicates and maintains a sequence, while a Set is designed for unique elements.
2. What is the difference between HashMap and TreeMap?
HashMap is designed for key-based lookup without sorted-key semantics, while TreeMap maintains its keys in sorted order.
3. When would you use ArrayList?
When you need a general-purpose ordered collection with efficient index-based access.
4. Why are generics used with collections?
They provide compile-time type safety and reduce the need for unsafe casts.
5. Is Map a Collection?
No. Map represents key-value associations and is separate from the Collection hierarchy.
6. When would you use a Set?
When duplicate values should not be stored.
7. What is a Queue?
A collection designed for holding elements before they are processed, with ordering determined by the queue implementation.
Final Takeaway
The Java Collections Framework becomes much easier when you stop memorizing classes and start thinking about the problem.
Need ordered data?
↓
List
Need unique data?
↓
Set
Need key-value lookup?
↓
Map
Need processing order?
↓
QueueThen choose the implementation based on additional requirements such as:
- Sorting
- Lookup patterns
- Memory usage
- Ordering
- Insertion and removal behavior
- Application workload
For most developers, learning List, Set, Map, and Queue well is far more valuable than trying to memorize every collection class.
Key Takeaways
Listis useful for ordered collections and allows duplicates.Setis useful when values must be unique.Mapstores key-value relationships.Queuerepresents items waiting to be processed.ArrayList,HashSet, andHashMapare common general-purpose choices.TreeSetandTreeMapare useful when sorted ordering matters.- Generics provide compile-time type safety.
- Choose a collection based on how your application uses the data.
- Good collection choices make Java applications easier to maintain and reason about.







Comments (0)
Be the first to share your thoughts.