Java Collections: Real-World Scenarios,
Approaches, and Solutions
Table of Contents
1. List (ArrayList, LinkedList)
2. Set (HashSet, LinkedHashSet, TreeSet)
3. Map (HashMap, TreeMap, LinkedHashMap)
4. Queue (PriorityQueue, LinkedList)
5. Stack (Stack class)
6. Dictionary (Map Interface)
1. List (ArrayList, LinkedList)
1.1 .add()
Scenario: A student selects courses for registration. The selected courses need to be added
to their registration list. This can be done using the .add() method.
Code:
import [Link].*;
public class CourseRegistration {
public static void main(String[] args) {
List<String> courseList = new ArrayList<>();
[Link]("Data Structures");
[Link]("Operating Systems");
[Link]("Registered Courses: " + courseList);
}
}
Output: [Data Structures, Operating Systems]
1.2 .get(index)
Scenario: After registration, the student wants to see the second course they enrolled in.
Code:
String secondCourse = [Link](1);
[Link]("Second Enrolled Course: " + secondCourse);
Output: Operating Systems
1.3 .set(index, value)
Scenario: The student decides to change their second course to a new course.
Code:
[Link](1, "Computer Networks");
[Link]("Updated Course List: " + courseList);
Output: [Data Structures, Computer Networks]
1.4 .remove()
Scenario: The student drops the course ‘Computer Networks’.
Code:
[Link]("Computer Networks");
[Link]("Updated Course List: " + courseList);
Output: [Data Structures]
1.5 .contains()
Scenario: Before adding a course, check if the student is already enrolled.
Code:
if ([Link]("Data Structures")) {
[Link]("Already Enrolled in Data Structures");
} else {
[Link]("Data Structures");
}
Output: Already Enrolled in Data Structures
2. Set (HashSet, LinkedHashSet, TreeSet)
2.1 .add()
Scenario: The system tracks unique email addresses for a newsletter subscription. When a
user subscribes, their email should be added.
Code:
Set<String> emails = new HashSet<>();
[Link]("user@[Link]");
[Link]("user@[Link]"); // Duplicate, ignored
[Link](emails);
Output: [user@[Link]]
2.2 .contains()
Scenario: Check if an email is already subscribed.
Code:
[Link]([Link]("user@[Link]"));
Output: true
2.3 .remove()
Scenario: A user unsubscribes from the newsletter.
Code:
[Link]("user@[Link]");
[Link](emails);
Output: []
3. Map (HashMap, TreeMap, LinkedHashMap)
3.1 .put()
Scenario: Store student information with their ID and name.
Code:
Map<Integer, String> students = new HashMap<>();
[Link](1001, "Alice");
[Link](1002, "Bob");
[Link](students);
Output: {1001=Alice, 1002=Bob}
3.2 .get()
Scenario: Retrieve the name of the student with a specific ID.
Code:
[Link]([Link](1001));
Output: Alice
3.3 .remove()
Scenario: Remove a student who has left the school.
Code:
[Link](1002);
[Link](students);
Output: {1001=Alice}
4. Queue (PriorityQueue, LinkedList)
4.1 .add()
Scenario: People enter a queue at a bank. The first person in line is served first.
Code:
Queue<String> queue = new LinkedList<>();
[Link]("Person1");
[Link]("Person2");
[Link](queue);
Output: [Person1, Person2]
4.2 .poll()
Scenario: The first person in line is served.
Code:
[Link]([Link]());
[Link](queue);
Output: Person1
[Person2]
4.3 .peek()
Scenario: See who is next in line.
Code:
[Link]([Link]());
Output: Person2
5. Stack (Stack class)
5.1 .push()
Scenario: You open a series of web pages in a browser. The last opened tab should be the
first to close.
Code:
Stack<String> browserTabs = new Stack<>();
[Link]("Home");
[Link]("About");
[Link](browserTabs);
Output: [Home, About]
5.2 .pop()
Scenario: Close the last opened tab (About).
Code:
[Link]([Link]());
[Link](browserTabs);
Output: About
[Home]
5.3 .peek()
Scenario: See which tab is currently open.
Code:
[Link]([Link]());
Output: Home
6. Dictionary (Map Interface)
6.1 .put()
Scenario: Store phone numbers of employees in a company directory.
Code:
Map<String, String> phoneBook = new HashMap<>();
[Link]("John", "123-456-7890");
[Link]("Jane", "987-654-3210");
[Link](phoneBook);
Output: {John=123-456-7890, Jane=987-654-3210}
6.2 .get()
Scenario: Retrieve the phone number of a specific employee.
Code:
[Link]([Link]("John"));
Output: 123-456-7890