This article provides a collection of frequently asked interview questions and answers regarding Java HashMap.
- Can we synchronize a HashMap ?
- Can we add duplicate keys in a HashMap ?
- What happens to the value if we add a duplicate key with new value ? Is is discarded or is it overwritten ?
- Can we add null keys in a HashMap ? Howmany null keys can we add ?
- What is the default load factor of HashMap ?
- What is the difference between a HashMap and a Hashtable ?
- What is an IdentityHashMap ? How is it different from a normal HashMap ?
- What is a WeakHashMap ?
- What is the difference between remove() and clear() methods ?
- Explain how HashMap works ?
- What is the difference between ConcurrentHashMap and Hashtable ?
- What is the difference between ConcurrentHashMap and Collections.synchronizedMap ?
- Provide a real-world example of HashMap
Please refer below the answers to these HashMap related questions :
Can we synchronize a HashMap ?
Yes, a HashMap can be synchronized using its synchronizedMap() method.
Map s = Collections.synchronizedMap(new HashMap(…));
Can we add duplicate keys in a HashMap ?
What happens to the value if we add a duplicate key with new value ? Is is discarded or is it overwritten ?
If a key already exists in a HashMap and we try to add another value for the same key, then the old value of the key will be replaced with the new value.
Read more about HashMap
Can we add null keys in a HashMap ? Howmany null keys can we add ?
We can add a single null key in a HashMap.
What is the default load factor of HashMap ?
Default load factor of HashMap is 0.75.
What is the difference between a HashMap and a Hashtable ?
– HashMap is not synchronized whereas Hashtable is synchronized.
– HashMap allows a null key and null values. Hashtable does not allow null keys or values.
– Since HashMap isn’t synchronized, it is faster compared to a HashTable.
What is an IdentityHashMap ? How is it different from a normal HashMap ?
IdentityHashMap is similar to HashMap except that it uses reference equality when comparing elements.
Instead of using the equals() method to compare entries as in HashMap, IdentityHashMap compares them using the == operator
So, two object references are considered equal if they refer to the same object instance.
Read more about IdentityHashMap
What is a WeakHashMap ?
A WeakHashMap implements a map that uses “weak keys”.
This allows an element in the map to be garbage-collected when the key is unused.
Read more about WeakHashMap
What is the difference between remove() and clear() methods in HashMap ?
We can remove entries from HashMap using remove(key) or clear() methods.
remove(key) removes the mapping for the key specified in parameter.
clear() method removes all the entries from the HashMap and returns void.
Explain how HashMap works ?
HashMap works on the principle of hashing.
put() and get() methods are used to store and retrieve data from hashmap.
It has a number of buckets which stores key-value pairs.
when put() is called, hashmap implementation calls hashcode() on the key to identify the bucket location… then stores both key+value in the bucket
when get() is called, the hashcode() on key is used to identify the bucket location and the value if returned.
If two key objects have same hashcode, bucket location will be the same and collision occurs in hashmap.
Inside the bucket, data is stored in linked list, so in collision scenario, it will get added to next node.
So, when get() is called, the hashcode() would point to the bucket location, then the use key.equals() to find the correct node in the linked list.
What is the difference between ConcurrentHashMap and Hashtable ?
Both can be used in multithreaded environment but once the size of Hashtable becomes considerable large performance degrade because for iteration it has to be locked for longer duration.
Since ConcurrentHashMap introduced concept of segmentation , no matter how large it becomes only certain part of it get locked to provide thread safety so many other readers can still access map without waiting for iteration to complete.
In Summary ConcurrentHashMap only locked certain portion of Map while Hashtable lock full map while doing iteration.
Read more about ConcurrentHashMap
What is the difference between ConcurrentHashMap and Collections.synchronizedMap ?
ConcurrentHashMap is designed for concurrency and improve performance while HashMap which is non synchronized by nature can be synchronized by applying a wrapper using Collections.synchronizedMap.
Provide a real-world example of using HashMap
Caches for static data is normally implemented using HashMap.
The key of the map is normally a string using which the data is accessed and the value is an object corresponding to a record of the static data.
© 2016, www.topjavatutorial.com. All rights reserved. On republishing this post, you must provide link to original post