Can
we synchronize a HashMap?
Yes, a HashMap can be synchronized using its
synchronizedMap() method.
Map s = Collections.synchronizedMap(new HashMap(…));
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.
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.
– 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.
- Iterator in hashmap is fail-fast. But enum in
Hashtable is not fail-fast. Fail-fast means if one thread is iterating over a
map and another is trying to modify it by adding/deleting elements, it will
throw ConcurrentModificationException
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.
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.
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.
This allows an element in the map to be garbage-collected when the key is unused.
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.
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.
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.
What is the difference between ConcurrentHashMap and Collections.synchronizedMap ?
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.
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.
No comments:
Post a Comment