Java Collections : Assignment
Question 1. Implement the following exercises on your Java IDE and also write
the TestClass with main function.
Exercise 1: Working with ArrayList
Task:
Create an ArrayList of strings. Add five city names. Perform the following operations:
Print the list.
Remove the third city.
Update the second city name.
Check if a particular city (e.g., "New York") is in the list.
Sort the list alphabetically and print it.
Solution:
import [Link];
import [Link];
public class ArrayListExercise {
public static void main(String[] args) {
ArrayList<String> cities = new ArrayList<>();
// Add five cities
[Link]("Paris");
[Link]("London");
[Link]("New York");
[Link]("Tokyo");
[Link]("Berlin");
// Print original list
[Link]("Cities: " + cities);
// Remove the third city (index 2)
[Link](2);
// Update the second city (index 1)
[Link](1, "Amsterdam");
// Check if "New York" is in the list
boolean containsNY = [Link]("New York");
[Link]("Contains New York? " + containsNY);
// Sort the list
[Link](cities);
[Link]("Sorted cities: " + cities);
}
}
Exercise 2: Using HashSet to remove duplicates
Task:
Create a HashSet from an array of integers with some duplicates. Print the set to show duplicates
removed.
Solution:
import [Link];
public class HashSetExercise {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 2, 4, 5, 3, 6, 1, 7};
HashSet<Integer> uniqueNumbers = new HashSet<>();
for (int num : numbers) {
[Link](num);
}
[Link]("Unique numbers: " + uniqueNumbers);
}
}
Exercise 3: Using HashMap for counting word frequency
Task:
Read a sentence, split it into words, and use a HashMap<String, Integer> to count the
frequency of each word. Print the result.
Solution:
import [Link];
public class HashMapExercise {
public static void main(String[] args) {
String sentence = "java collections are powerful and java collections are easy to
use";
String[] words = [Link](" ");
HashMap<String, Integer> wordCount = new HashMap<>();
for (String word : words) {
[Link](word, [Link](word, 0) + 1);
}
[Link]("Word frequencies: " + wordCount);
}
}
Exercise 4: LinkedList operations
Task:
Create a LinkedList of integers and perform:
Add elements at the beginning and end.
Remove the first and last elements.
Iterate and print elements.
Solution:
import [Link];
public class LinkedListExercise {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();
// Add elements
[Link](10);
[Link](20);
[Link](30);
// Add at beginning and end
[Link](5);
[Link](40);
[Link]("List after additions: " + list);
// Remove first and last
[Link]();
[Link]();
[Link]("List after removals: " + list);
// Iterate and print
for (int num : list) {
[Link](num);
}
}
}
Question2: Read the scenario and implement the class with given tasks and write
the code of TestClass with main function.
Scenario:
You have a list of employees and need to perform various operations such as sorting, filtering by
department, and grouping by role.
Task:
Create an Employee class with fields: id (int), name (String), department (String), and
salary (double).
Store a list of employees using ArrayList.
Sort employees by salary in descending order.
Filter employees in a specific department (e.g., "IT").
Group employees by their department using a Map<String, List<Employee>>.
Find the employee with the highest salary.