0% found this document useful (0 votes)
5 views7 pages

Java_Collections_Scenarios

The document provides an overview of Java Collections, detailing various data structures such as Lists, Sets, Maps, Queues, and Stacks, along with practical scenarios and code examples for each. It demonstrates how to perform common operations like adding, retrieving, updating, and removing elements. Additionally, it includes examples of using the Map interface for storing key-value pairs, such as employee phone numbers.

Uploaded by

apsammu23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Java_Collections_Scenarios

The document provides an overview of Java Collections, detailing various data structures such as Lists, Sets, Maps, Queues, and Stacks, along with practical scenarios and code examples for each. It demonstrates how to perform common operations like adding, retrieving, updating, and removing elements. Additionally, it includes examples of using the Map interface for storing key-value pairs, such as employee phone numbers.

Uploaded by

apsammu23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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 java.util.*;

public class CourseRegistration {


public static void main(String[] args) {
List<String> courseList = new ArrayList<>();
courseList.add("Data Structures");
courseList.add("Operating Systems");
System.out.println("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 = courseList.get(1);
System.out.println("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:

courseList.set(1, "Computer Networks");


System.out.println("Updated Course List: " + courseList);

Output: [Data Structures, Computer Networks]

1.4 .remove()
Scenario: The student drops the course ‘Computer Networks’.

Code:

courseList.remove("Computer Networks");
System.out.println("Updated Course List: " + courseList);

Output: [Data Structures]

1.5 .contains()
Scenario: Before adding a course, check if the student is already enrolled.

Code:

if (courseList.contains("Data Structures")) {
System.out.println("Already Enrolled in Data Structures");
} else {
courseList.add("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<>();


emails.add("[email protected]");
emails.add("[email protected]"); // Duplicate, ignored
System.out.println(emails);

Output: [[email protected]]

2.2 .contains()
Scenario: Check if an email is already subscribed.

Code:

System.out.println(emails.contains("[email protected]"));

Output: true

2.3 .remove()
Scenario: A user unsubscribes from the newsletter.

Code:

emails.remove("[email protected]");
System.out.println(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<>();
students.put(1001, "Alice");
students.put(1002, "Bob");
System.out.println(students);

Output: {1001=Alice, 1002=Bob}

3.2 .get()
Scenario: Retrieve the name of the student with a specific ID.

Code:

System.out.println(students.get(1001));

Output: Alice

3.3 .remove()
Scenario: Remove a student who has left the school.

Code:

students.remove(1002);
System.out.println(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<>();


queue.add("Person1");
queue.add("Person2");
System.out.println(queue);
Output: [Person1, Person2]

4.2 .poll()
Scenario: The first person in line is served.

Code:

System.out.println(queue.poll());
System.out.println(queue);

Output: Person1
[Person2]

4.3 .peek()
Scenario: See who is next in line.

Code:

System.out.println(queue.peek());

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<>();


browserTabs.push("Home");
browserTabs.push("About");
System.out.println(browserTabs);

Output: [Home, About]

5.2 .pop()
Scenario: Close the last opened tab (About).
Code:

System.out.println(browserTabs.pop());
System.out.println(browserTabs);

Output: About
[Home]

5.3 .peek()
Scenario: See which tab is currently open.

Code:

System.out.println(browserTabs.peek());

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<>();


phoneBook.put("John", "123-456-7890");
phoneBook.put("Jane", "987-654-3210");
System.out.println(phoneBook);

Output: {John=123-456-7890, Jane=987-654-3210}

6.2 .get()
Scenario: Retrieve the phone number of a specific employee.

Code:

System.out.println(phoneBook.get("John"));
Output: 123-456-7890

You might also like