Hashmap Interview Questions & Answers

5 avg. rating (100% score) - 1 votes

Hashmap Interview Questions & Answers

Looking for Hashmap job? Prepare by practicing Hashmap job interview questions and answers designed by wisdom jobs professional experts. Maps are used when you want to connect value with a key and lists are an ordered set. The map is an interface in the Java Collection Framework and a HashMap is the execution of the Map interface. HashMap is capable of pointing a value based on a key and deleting and inserting values based on a key. There is the huge demand for Hashmap jobs in famous cities like Delhi, Mumbai, Rajasthan, Hyderabad, Chennai, Bangalore, Pune etc. Go through our Hashmap job interview questions and answers page that can help you to get ready to answer job-related interview questions and answers.

Hashmap Interview Questions

Hashmap Interview Questions
    1. Question 1. How Does Get() Method Of Hashmap Works In Java?

      Answer :

      The get() method of HashMap works in the principle of hashing. It is responsible for storing an object into backend array. The hashcode() method is used in conjunction with a hash function to find the correct location for the object into the bucket. If a collision occurs then the entry object which contains both key and value is added to a linked list and that linked list is stored into the bucket location.

    2. Question 2. What Is The Requirement For An Object To Be Used As Key Or Value In Hashmap?

      Answer :

      The key or value object must implement equals() and hashcode() method. The hash code is used when you insert the key object into the map while equals are used when you try to retrieve a value from the map.

    3. Question 3. What Will Happen If You Try To Store A Key Which Is Already Present In Hashmap?

      Answer :

      If you store an existing key in the HashMap then it will override the old value with the new value and put() will return the old value. There will not be any exception or error.

    4. Question 4. Can You Store A Null Key In Java Hashmap?

      Answer :

      Yes, HashMap allows one null key which is stored at the first location of bucket array e.g. bucket[0] = value. The HashMap doesn't call hashCode() on null key because it will throw NullPointerException, hence when a user call get() method with null then the value of the first index is returned.

    5. Question 5. Can You Store A Null Value Inside Hashmap In Java?

      Answer :

      Yes, HashMap also allows null value, you can store as many null values as you want as shown in the hashmap example post in this blog.

    6. Question 6. How Does Hashmap Handle Collisions In Java?

      Answer :

      The java.util.HashMap uses chaining to handle collisions, which means new entries, an object which contains both key and values, are stored in a linked list along with existing value and then that linked list is stored in the bucket location. In the worst case, where all key has the same hashcode, your hash table will be turned into a linked list and searching a value will take O(n) time as opposed to O(1) time.

    7. Question 7. Which Data Structure Hashmap Represents?

      Answer :

      The HashMap is an implementation of hash table data structure which is idle for mapping one value to other e.g. id to name as you can search for value in O(1) time if you have the key.

    8. Question 8. Which Data Structure Is Used To Implement Hashmap In Java?

      Answer :

      Even though HashMap represents a hash table, it is internally implemented by using an array and linked list data structure in JDK. The array is used as bucket while a linked list is used to store all mappings which land in the same bucket. From Java 8 onwards, the linked list is dynamically replaced by binary search tree, once a number of elements in the linked list cross a certain threshold to improve performance.

    9. Question 9. Can You Store A Duplicate Key In Hashmap?

      Answer :

      No, you cannot insert duplicate keys in HashMap, it doesn't allow duplicate keys. If you try to insert an existing key with new or same value then it will override the old value but size of HashMap will not change i.e. it will remain same. This is one of the reason when you get all keys from the HashMap by calling keySet() it returns a Set, not a Collection because Set doesn't allow duplicates.

    10. Question 10. Can You Store The Duplicate Value In Java Hashmap?

      Answer :

      Yes, you can put duplicate values in HashMap of Java. It allows duplicate values, that's why when you retrieve all values from the Hashmap by calling values() method it returns a Collection and not Set. Worth noting is that it doesn't return List because HashMap doesn't provide any ordering guarantee for key or value.

    11. Question 11. Is Hashmap Thread-safe In Java?

      Answer :

      No, HashMap is not thread-safe in Java. You should not share an HashMap with multiple threads if one or more thread is modifying the HashMap e.g. inserting or removing a map. Though, you can easily share a read-only HashMap.

    12. Question 12. What Will Happen If You Use Hashmap In A Multithreaded Java Application?

      Answer :

      If you use HashMap in a multithreaded environment in such a way that multiple threads structurally modify the map e.g. add, remove or modify mapping then the internal data structure of HashMap may get corrupt i.e. some links may go missing, some may point to incorrect entries and the map itself may become completely useless. Hence, it is advised not to use HashMap in the concurrent application, instead, you should use a thread-safe map e.g. ConcurrentHashMap or Hashtable

    13. Question 13. What Are Different Ways To Iterate Over Hashmap In Java?

      Answer :

      Here are some of the ways to iterate over HashMap in Java:

      • by using keySet and iterator
      • by using entrySet and iterator
      • by using entrySet and enhanced for loop
      • by using keySet and get() method

    14. Question 14. How Do You Remove A Mapping While Iterating Over Hashmap In Java?

      Answer :

      Even though HashMap provides remove() method to remove a key and a key/value pair, you cannot use them to remove a mapping while traversing an HashMap, instead, you need to use the Iterator's remove method to remove a mapping as shown in the following example:

      Iterator itr = map.entrySet().iterator();

      while(itr.hasNext()){

      Map.Entry current = itr.next();

      if(current.getKey().equals("matching"){

      itr.remove(); // this will remove the current entry.

      }

      }

      You can see that we have used Iterator.remove() method to remove the current entry while traversing the map.

    15. Question 15. In Which Order Mappings Are Stored In Hashmap?

      Answer :

      Random order because HashMap doesn't provide any ordering guarantee for keys, values, or entries. When you iterate over an HashMap, you may get the different order every time you iterate over it.

    16. Question 16. Can You Sort Hashmap In Java?

      Answer :

      No, you cannot sort a HashMap because unlike List it is not an ordered collection. Albeit, you can sort contents of HashMap by keys, values or by entries by sorting and then storing the result into an ordered map e.g. LinkedHashMap or a sorted map e.g. TreeMap.

    17. Question 17. What Is Load Factor In Hashmap?

      Answer :

      A load factor is a number which controls the resizing of HashMap when a number of elements in the HashMap cross the load factor e.g. if the load factor is 0.75 and when becoming more than 75% full then resizing trigger which involves array copy.

    18. Question 18. How Does Resizing Happens In Hashmap?

      Answer :

      The resizing happens when map becomes full or when the size of map crosses the load factor. For example, if the load factor is 0.75 and when become more than 75% full then resizing trigger which involves array copy. First, the size of the bucket is doubled and then old entries are copied into a new bucket.

    19. Question 19. How Many Entries You Can Store In Hashmap? What Is The Maximum Limit?

      Answer :

      There is no maximum limit for HashMap, you can store as many entries as you want because when you run out of the bucket, entries will be added to a linked list which can support an infinite number of entries, of course until you exhaust all the memory you have.

      Btw, the size() method of HashMap return an int, which has a limit, once a number of entries cross the limit, size() will overflow and if your program relies on that then it will break. This issue has been addressed in JDK 8 by introducing a new method called mappingCount() which returns a long value. So, you should use mappingCount() for large maps. See Java SE 8 for Really Impatient to learn more about new methods introduced in existing interfaces in JDK 8.

    20. Question 20. What Is The Difference Between Capacity And Size Of Hashmap In Java?

      Answer :

      The capacity denotes how many entries HashMap can store and size denotes how many mappings or key/value pair is currently present.

    21. Question 21. What Will Happen If Two Different Keys Of Hashmap Return Same Hashcode()?

      Answer :

      If two keys of HashMap return same hash code then they will end up in the same bucket, hence collision will occur. They will be stored in a linked list together.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java 8 Tutorial