10 Top Hashmap Interview Questions With Sample Answers
Updated 30 September 2022
The Indeed Editorial Team comprises a diverse and talented team of writers, researchers and subject matter experts equipped with Indeed's data and insights to deliver useful tips to help guide your career journey.
A HashMap is an unsynchronised Java class in the Java Collection Framework, and its object contains null values and keys. When employers interview software engineers, they expect them to know about HashMap and other fundamentals of computer architecture. By understanding the types of technical questions you might encounter at a job interview, you can prepare effective responses to improve your chances of getting the job. In this article, we provide a list of frequently-asked HashMap interview questions and refer to a few sample answers to help you with your interview preparation.
HashMap interview questions with sample answers
The following HashMap interview questions and sample answers may help you with your job interview preparation:
1. How does HashMap differ from Hashtable in Java ?
Interviewers often ask this question to find out if you understand how to correctly use collection classes. You can list the differences between HashMap and Hashtable in your response.
Example: 'There are various differences between HashMap and Hashtable. While HashMan is unsynchronised and not thread-safe, Hashtable is synchronised and threadsafe. You have only one null key and innumerable null values in HashMap, but the Hashtable object cannot have any null keys and null values. An iterator is necessary for iterating HashMap object values, while Hashtable uses an enumerator to iterate Hashtable object values. The iterator is a fail-fast one, but the enumerator is not. As HashMap is unsynchronised, it's faster and uses less memory than the synchronised Hashtable. It performs better than Hashtable in a single-thread environment.'
Related: Top 50 Java Interview Questions For Experienced Programmers
2. What happens if you store a duplicate key in HashMap?
When the interviewer asks you this question, it is to assess your experience of using HashMap. You can provide a brief explanation about what happens to the value of the key when you store a duplicate one in HashMap. You can also mention why the keys return to Set rather than to Collection.
Example: 'When you store a duplicate key in the HashMap, it does not throw an error or an exception. Nor does the size of the HashMap change. The value of the existing key just gets replaced by the value of the new key. When you call the keySet() method on HashMap, the keys return to Set instead of Collection. That is because Set does not allow for duplicates.'
3. Can you explain the difference between HashMap and ArrayList ?
The interviewer may ask you to differentiate between HashMap and ArrayList to assess your understanding of the two most commonly used classes in the Java Collection Framework. In your answer, you can explain the differences in how they store and process data.
Example: 'The HashMap class can implement Map interface in Java and ArrayList implements the List interface. There is no guarantee that the insertion order can prevail with HashMap and the returned key-values pair in any order. ArrayList maintains the insertion order and returns the list items in the same order. HashMap stores elements with key and value pairs, which means it stores two objects and requires more memory for doing so. Along with storing the elements as values, ArrayList maintains an internal index of each element. You cannot have duplicate keys in HashMap, although it allows duplicate values. Duplicate elements are possible in ArrayList.'
4. How do you measure the performance of HashMap?
As a software engineer working in Java, it is essential you know how to measure the performance of HashMap. When the interviewer asks you this question, you can mention the parameters you use for the performance measurement. You can elaborate on what these are and how they help.
Example: 'You can measure the performance of HashMap by looking at the parameters that affect it. These are initial capacity and load factor. Capacity is the number of buckets in the hash table has. Initial capacity indicates the number of buckets the hash table had at the time of its creation. Load factor is the measure that determines how full the hash table is and when to increase the Map capacity. The default load factor is 75% of the capacity.'
Related: 100 Java Interview Questions For Freshers With Example Answers
5. Can you use HashMap in a multithreaded Java application?
The interviewer may ask this question to learn about your experience using HashMap in Java applications. You can explain whether HashMap is useful in a multithreaded Java application.
Example: 'It is possible to use HashMap in a multithreaded environment but the multiple threads operating simultaneously in HashMap can modify the map structure by adding or removing mapping. That can corrupt the HashMap internal data structure. It can lead to incorrect entries, missing links and other errors. The map itself may eventually become unusable. So, HashMap is not the preferred choice in a multithreaded environment.'
Related: 30 Java Multithreading Interview Questions And Answers
6. How would you design a good key for HashMap?
By asking this question, the interviewer wants to know whether you have the skill they require for the available position. In your response, you can explain the basic purpose of designing a good key. You can also mention how you would make the HashMap key more effective.
Example: 'When you design a good key, you want to make sure that it can easily retrieve the value object from the map. If it cannot do that, the data structure you build can be of no use. You can override the hashCode() method to make sure the key is immutable. Then, when the key object's state changes, it does not change the object's hash code. To ensure there are no undesirable behaviors during run time, you can check that the key class honours the the hashCode() and equals() methods.'
Related: What Does A Java Developer Do? And How to Become One
7. Can you explain a collision in HashMap and how to resolve it?
When interviewers ask this question, the interviewer wants to know whether you have the skill they require for the available position. You can respond by explaining the basic purpose of designing a good key. You can also mention how you would make the HashMap key more effective.
Example: 'When you design a good key, you want to make sure that it can easily retrieve the value object from the map. If it fails to retrieve it, the data structure you build is not of much use. You can override the hashCode() method to make sure the key is immutable. Then, when the key object's state changes, it does not change the object's hash code. To ensure no undesirable behaviors arise during run time, you can check that the key class honours the hashCode() and equals() methods.'
8. How does HashMap differ from ConcurrentHashMap in Java?
This is a frequently-asked technical question in a Java job interview. When interviewers ask this question, they want to test your understanding of the concurrency concept. You can give a few examples of how HashMap differs from ConcurrentHashMap in your answer.
Example: 'There are several differences between HashMap and ConcurrentHashMap. HashMap is a class that is part of Traditional Collections. The Concurrent Collections contain the ConcurrentHashMap class. HashMap is not thread-safe in nature, but ConcurrentHashMap is thread-safe. Since HashMap is unsynchronised and allows multiple threads to perform simultaneously, it can give a high performance. The performance of ConcurrentHashMap is relatively low. HashMap has one null key, while the keys in ConcurrentHashMap cannot contain null values.
You can use the synchronisedMap(HashMap) method to synchronise HashMap and get HashMap object equal to the Hashtable object. That ensures that every Map modification gets locked on the Map object. You can partition the Map as per the Concurrency level to optimise the ConcurrentHashMap performance. Since it synchronises on a specific Map portion, it is not necessary to synchronise the entire Map Object.**'
9. Is it possible to sort HashMap in Java?
Interviewers often ask this question in Java job interviews. You can explain if sorting is possible and the reason for it. You can also explain what you can do to sort the contents of HashMap.
Example: 'HashMap is not an ordered collection, so it is not possible to sort it in Java. You can still sort the HashMap content based upon keys, values or entries. You can copy entries from HashMap to create a TreeMap and use it to sort the keys. You can also retain the insertion order by creating a LinkedHashMap. You can create a Comparator to compare elements based on values.'
10. How does HashMap differ from TreeMap in Java?
Interviewers ask this question to test your understanding of how HashMap and TreeMap work in Java. You can explain a few differences between them in your response.
Example: 'HashMap does not maintain any order guarantee, so you cannot be sure that the element inserted first prints first. TreeMap sorts elements as per their natural order. You can use the compareTo() method for sorting the TreeMap object elements if it is not possible to sort them in a natural order. For implementation, HashMap uses Hashing internally. TreeMap uses Red-Black tree implementation internally. HashMap can contain only one null key and innumerable null values, while TreeMap can contain many null values but no null keys.'
Please note that none of the companies, institutions or organisations mentioned in this article are associated with Indeed.
Explore more articles
- SAP GRC Interview Questions (With Sample Answers And Tips)
- 'Do You Want To Ask A Specific Question?' In An Interview
- “Tell Us About The Last Book You Read” Interview Question
- 7 Kitchen Interview Questions And Answers To Prepare
- 8 Communication Interview Questions (Plus Sample Answers)
- 30 System Design Interview Questions (With Example Answers)
- 35 Project Coordinator Interview Questions (With Examples)
- Document Specialist Interview Questions With Sample Answers
- IT Technical Interview Questions (With Sample Answers)
- 'Who Is Your Role Model?' (Interview Question With Answers)
- 36 Agile Project Manager Interview Questions (With Answers)
- IT Recruiter Interview Questions (With Sample Answers)