Java HashMap Practice Assignment
Easy to Tricky Questions
This assignment is designed to help students master Java HashMap concepts from beginner to
interview level. Attempt all questions before looking at solutions.
Q1. Create a HashMap of Integer and String. Add 5 student IDs and names, then print the map.
Q2. Retrieve the value associated with a given key from a HashMap.
Q3. Check whether a particular key exists in the HashMap.
Q4. Check whether a particular value exists in the HashMap.
Q5. Update the value of an existing key and print the updated map.
Q6. Remove a key-value pair from the HashMap and display the remaining entries.
Q7. Iterate through all entries using entrySet() and print key and value separately.
Q8. Find the size of a HashMap and print all keys and all values separately.
Q9. Count the frequency of each character in a given string using HashMap.
Q10. Count the frequency of each word in a sentence using HashMap.
Q11. Given an array of integers, find the first non-repeating element using HashMap.
Q12. Given an array of integers, find all duplicate elements using HashMap.
Q13. Create an Employee class (id, name, salary) and store Employee objects in a HashMap. Print
employees with salary greater than 50,000.
Q14. Given two strings, determine whether they are anagrams using HashMap.
Q15. Design a mini student result system using HashMap where roll number is the key and marks
are the value. Find the topper, average marks, and failed students.
Bonus Interview Questions
• What is the difference between HashMap, LinkedHashMap, and TreeMap?
• Why is the average time complexity of get() and put() O(1)?
• How does HashMap handle collisions internally?
• What happens if two keys have the same hashCode()?
• Why must equals() and hashCode() be overridden together?