Coding Problems in Java Programming by Tawqeer ul Islam
Coding Problems in Java – Set 4
1. Add and Retrieve Elements from an ArrayList
Problem:
• Create an ArrayList<String>, add five names to it, and print each name using a
loop.
Hint: Use add() and get() methods of ArrayList.
2. Remove an Element from a List
Problem:
• Given a List<Integer>, remove all occurrences of a specific number.
List<Integer> numbers = Arrays.asList(1, 2, 3, 2, 4, 2, 5);
int target = 2;
System.out.println(removeElement(numbers, target));
// Output: [1, 3, 4, 5]
Hint: Use removeIf() method.
3. Check if a List Contains a Specific Element
Problem:
• Given a List<String>, check if a specific string is present.
Hint: Use the contains() method of List.
4. Convert an Array to a List and Vice Versa
Problem:
• Convert an Integer[] array into a List<Integer> and back to an array.
Hint: Use Arrays.asList() and toArray().
5. Find the Maximum and Minimum Elements in a List
Problem:
• Given a List<Integer>, find and print the maximum and minimum values.
Hint: Use Collections.max() and Collections.min().
Coding Problems in Java Programming by Tawqeer ul Islam
6. Reverse a List
Problem:
• Given a List<String>, reverse its elements.
Hint: Use Collections.reverse().
7. Sort a List in Ascending and Descending Order
Problem:
• Given a List<Integer>, sort it in both ascending and descending order.
Hint: Use Collections.sort() and Collections.reverseOrder().
8. Count the Frequency of Elements in a List
Problem:
• Given a List<String>, count how many times each string appears.
List<String> words = Arrays.asList("apple", "banana", "apple", "orange", "banana",
"apple");
System.out.println(countFrequencies(words));
// Output: {apple=3, banana=2, orange=1}
Hint: Use a HashMap<String, Integer>.
9. Find Common Elements Between Two Lists
Problem:
• Given two List<Integer>, find the common elements.
List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> list2 = Arrays.asList(3, 4, 5, 6, 7);
System.out.println(findCommonElements(list1, list2));
// Output: [3, 4, 5]
Hint: Use retainAll() method.
10. Implement a Stack Using Deque
Problem:
• Use Deque<Integer> to implement a stack with:
o push(int x)
Coding Problems in Java Programming by Tawqeer ul Islam
o pop()
o peek()
o isEmpty()
Hint: Use ArrayDeque<Integer> with addFirst() and removeFirst().
Thodi Badi Problem
Problem: Student Management System Using Collections
Problem Statement: You need to create a simple Student Management System in Java
using the Collections Framework. The system should allow users to perform the following
operations:
1. Add a student with details:
o id (Integer)
o name (String)
o age (Integer)
o marks (Double)
2. Remove a student by their id.
3. Search for a student by their id and display their details.
4. Display all students, sorted by their marks in descending order.
5. Find the student with the highest marks.
Implementation Hints:
• Use a HashMap<Integer, Student> where the key is the student's id and the value
is the Student object.
• Use Collections.sort() with a Comparator to sort students by marks.
• Implement the operations inside a menu-driven program using a Scanner for user
input.
Expected Output Example:
1. Add Student
2. Remove Student
3. Search Student
4. Display All Students
5. Find Top Student
Coding Problems in Java Programming by Tawqeer ul Islam
6. Exit
Enter your choice: 1
Enter ID: 101
Enter Name: John
Enter Age: 20
Enter Marks: 85.5
Student added successfully!
Enter your choice: 4
Students List (Sorted by Marks):
ID: 101, Name: John, Age: 20, Marks: 85.5
Enter your choice: 5
Top Student:
ID: 101, Name: John, Age: 20, Marks: 85.5
Enter your choice: 6
Exiting...