0% found this document useful (0 votes)
3 views

Extended_Java_Collections_and_Map_Test_Paper

The document outlines a series of Java programming tasks focused on various collection types, including ArrayList, HashSet, TreeSet, HashMap, and more. Each question requires the implementation of specific functionalities, such as adding, removing, and sorting elements, while also posing questions about the characteristics and performance of these data structures. The tasks aim to deepen understanding of Java collections and their applications in real-world scenarios.

Uploaded by

nguyenhieuson6
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Extended_Java_Collections_and_Map_Test_Paper

The document outlines a series of Java programming tasks focused on various collection types, including ArrayList, HashSet, TreeSet, HashMap, and more. Each question requires the implementation of specific functionalities, such as adding, removing, and sorting elements, while also posing questions about the characteristics and performance of these data structures. The tasks aim to deepen understanding of Java collections and their applications in real-world scenarios.

Uploaded by

nguyenhieuson6
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Extended Java Collections and Map Test

Paper
Question 1: Basic List Operations with ArrayList
1. Write a Java program that performs the following operations on an ArrayList of integers:
- Add 10 integers (ranging from 1 to 100) to the ArrayList.
- Print the ArrayList.
- Retrieve and print the first, third, and last elements in the ArrayList.
- Remove the second element and print the updated ArrayList.

**Question:** What is the time complexity of the operations `add()`, `remove()`, and
`get()` in ArrayList? How does an ArrayList differ from other List implementations like
LinkedList?

Question 2: Working with HashSet and Sorting with TreeSet


1. Write a Java program that creates a HashSet of integers, adds some duplicates, and prints
the set to show that duplicates are not allowed.
- Then convert the HashSet into a TreeSet and print the TreeSet to demonstrate how
elements are sorted in ascending order.

**Question:** How does HashSet ensure uniqueness of elements? How does TreeSet
differ from HashSet in terms of ordering and performance? When would you choose one
over the other?

Question 3: Sorting a List of Custom Objects with Comparator


1. Write a Java program that creates an ArrayList of custom objects, such as a 'Book' class
with 'title', 'author', and 'price' fields.
- Implement a custom Comparator to sort the list by price in ascending order.
- Use Collections.sort() to sort the list and print the sorted list.

**Question:** What is the difference between implementing Comparable and


Comparator? In which scenarios would you use each? How does the Comparator affect the
sorting behavior?

Question 4: Working with HashMap for Word Frequency Count


1. Write a Java program that takes a sentence as input from the user and counts the
frequency of each word using a HashMap.
- Convert the sentence to lowercase, split it into words, and store the word frequencies
in a HashMap where the key is the word and the value is the count.

**Question:** How does HashMap handle case sensitivity and punctuation while
counting word frequencies? Can you modify the program to handle these issues efficiently?

Question 5: Sorting Data with TreeMap


1. Write a Java program that uses a TreeMap to store employee names (as keys) and their
salaries (as values).
- Sort the TreeMap by employee names and print the sorted data.

**Question:** How does a TreeMap ensure its elements are sorted? What are the key
differences between HashMap and TreeMap regarding performance and sorting? When
would you prefer one over the other?

Question 6: Using LinkedHashMap for Maintaining Insertion Order


1. Write a Java program that uses a LinkedHashMap to store product names (as keys) and
their prices (as values).
- Insert the products in the order they are added and print the contents of the
LinkedHashMap.

2. Remove a product by its key and print the updated map.

**Question:** How does LinkedHashMap maintain the order of elements? How does it
differ from HashMap, and when would you choose LinkedHashMap over HashMap?

Question 7: Sorting a List Using Streams


1. Write a Java program that uses streams to filter a list of custom objects (e.g., 'Person' class
with 'name' and 'age' fields).
- Filter the list to only include persons who are 18 years or older.
- Sort the filtered list by age in ascending order using streams and collect the results
into a new list.

Iterator<Person> iterator = list.iterator();

while (iterator.hasNext()) {

if (iterator.next().getAge() < 18) {

iterator.remove(); // Xóa phần tử một cách an toàn

}
}

**Question:** What advantages do streams offer for filtering and sorting collections?
How do streams improve code readability and performance in real-world applications?

Question 8: Advanced TreeMap Operations with Custom Comparator


1. Write a Java program that uses a TreeMap to store a set of student names (keys) and their
grades (values).
- Sort the TreeMap by grade values in descending order using a custom Comparator and
print the sorted data.

**Question:** How does TreeMap handle sorting based on values? What challenges
might arise when sorting values in TreeMap, and how can you use a custom Comparator to
solve this?

Question 9: Performance Comparison Between HashMap and TreeMap


1. Write a Java program that creates both a HashMap and a TreeMap to store the same set of
keys and values (e.g., employee ID and salary).
- Measure and compare the performance of inserting and retrieving data from both
maps.
- Print the time taken for each operation.

**Question:** How do HashMap and TreeMap differ in terms of time complexity for
insertion, retrieval, and deletion operations? In what scenarios would you prefer one over
the other in terms of performance?

Question 10: Handling Concurrent Data with ConcurrentHashMap


1. Write a Java program that uses a ConcurrentHashMap to store a shared counter (e.g.,
number of tickets sold) that is accessed and updated by multiple threads.
- Simulate a ticket sale where each thread increments the counter by 1.
- After all threads finish, print the final counter value from the ConcurrentHashMap.

**Question:** How does ConcurrentHashMap provide thread safety in a multithreaded


environment? How does it differ from using synchronized blocks or other thread-safe
collections like Hashtable?

Question 11: Working with PriorityQueue


1. Write a Java program that creates a PriorityQueue of integers and performs the following
operations:
- Add 10 random integers to the PriorityQueue.
- Print the PriorityQueue.
- Remove the top element and print the updated queue.

**Question:** How does a PriorityQueue maintain order? What is the time complexity
for insertion and removal in a PriorityQueue? When is it appropriate to use a PriorityQueue
in real-world applications?

Question 12: Advanced Sorting and Filtering with Lists


1. Write a Java program that uses streams to filter a list of objects based on multiple
conditions (e.g., filter persons by age > 18 and salary > 50000).
- Sort the filtered results by multiple attributes (e.g., first by age and then by salary).

**Question:** How can you sort and filter a collection based on multiple attributes?
How do streams handle this complexity, and what are the best practices for using streams
with complex filters and sorts?

Question 13: Using CopyOnWriteArrayList for Thread Safety


1. Write a Java program that uses a CopyOnWriteArrayList to store a list of integers.
- Simulate multiple threads accessing and modifying the list concurrently.
- After all threads have finished, print the final list.

**Question:** How does CopyOnWriteArrayList differ from ArrayList in terms of thread


safety and performance? When should you use CopyOnWriteArrayList instead of other
thread-safe collections?

Question 14: Using Deque for Efficient Data Operations


1. Write a Java program that uses a Deque (Double-Ended Queue) to perform the following
operations:
- Add elements to both ends of the queue.
- Remove elements from both ends of the queue.
- Print the contents of the queue after each operation.

**Question:** What are the advantages of using a Deque over a Queue or Stack? How
can a Deque improve the efficiency of operations where elements are added or removed
from both ends of a collection?

Question 15: Implementing a Custom Collection Class


1. Write a Java program that implements a custom collection class that behaves like a List.
- Implement the add, get, and remove methods.
- Provide methods to return the size of the list and check if the list is empty.
**Question:** How do you implement a custom collection class? What are the key
interfaces and methods that a custom collection should implement to adhere to the
Collection framework in Java?

You might also like