More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Ans. They are different the way their elements are stored in memory. TreeMap stores the Keys in order whereas HashMap stores the key value pairs randomly.
Ans. There can be two different elements with the same hashcode. When two elements have the same hashcode then Java uses the equals to further differentation. So there can be one or two objects depending on the content of the objects.
Help us improve. Please let us know the company, where you were asked this question :
Ans. HashTable has been deprecated. As an alternative, ConcurrentHashMap has been provided. It uses multiple buckets to store data and hence much better performance than HashTable. Moreover, there is already a raw type HashMap. The only difference between the HashTable and HashMap is that Hashtable is synchronized whereas HashMap is not. Most of the synchronized collections have been deprecated and their raw alternative have been presented as preferred.Synchronization has a cost. Using synchronized collection in places where there is no need of it leads to useless utilization of resources. As these collections are rarely used in a static context or shared among threads, Java might have thought it better to just provide the raw collection and let developers implement synchronization if he feels the need to do so. HashMap is now presented as the default and the preferred way of using Map with read optimized hashing, and ConcurrentHashMap has been provided for synchronized access which provides better performance than HashTable. Because of this, Java thought it right to deprecate the use of HashTable.'
Synchronization has a cost. Using synchronized collection at a place where there is hardly any need of it would means useless utilization of resources. As these collections are rarely used in static context or shared among threads, Java might have thought it better to just provide the raw collection and let developer implement synchronization if he feels the need to do so.
As HashMap has been presented as default and preferred way of using Map with read optimized hashing, and ConcurrentHashMap has been provided for synchronized access which provides better performance than HashTable, Java thought it right to deprecate the use of HashTable.
Help us improve. Please let us know the company, where you were asked this question :
LikeDiscussCorrect / Improve  hashtable  synchronized collections  Why synchronized collections have been deprecated  Why HashTable has been deprecated  HashTable vs HashMap expert
Ans. Since String is immutable, its hashcode is cached at the time of creation and it doesnt need to be calculated again. This makes it a great candidate for key in a Map and its processing is fast than other HashMap key objects. This is why String is mostly used Object as HashMap keys.
Help us improve. Please let us know the company, where you were asked this question :
If we try to add duplicate key to the HashMap, What will happen ?
It will throw an exception.
It won't add the new Element without any exception.
The new element will replace the existing element.
Compiler will identify the problem and will throw an error.
Q13. If we try to add duplicate key to the HashMap, What will happen ?
a. It will throw an exception. b. It won't add the new Element without any exception. c. The new element will replace the existing element. d. Compiler will identify the problem and will throw an error.
Ans. The new element will replace the existing element.
Help us improve. Please let us know the company, where you were asked this question :
Ans. WeakHashMap uses weak reference for keys, which means if the key object doesn't have any reference then both key/value mapping will become eligible for garbage collection.
Help us improve. Please let us know the company, where you were asked this question :
a. HashMap came before HashTable. b. HashMap allows null values whereas Hashtable doesn’t allow null values. c. HashTable and HashMap allow Key-Value pairs. d. Hashtable is synchronized whereas HashMap is not.
Ans. HashMap came before HashTable.
Help us improve. Please let us know the company, where you were asked this question :
Ans. IdentityHashMap is a class that implements AbstractMap and provides a data structure with Elements having Key Value pair, just like HashMap. It is similar to HashMap except that it uses reference equality when comparing elements.
Help us improve. Please let us know the company, where you were asked this question :
Ans. HashTable locks the complete collection to provide synchronization whereas ConcurrentHashMap only locks segments to achieve synchronization and hence better efficient and faster.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Yes, they both aim at providing synchronized access to the Map collection. The only difference is in their implementation. ConcurrentHashMap came after HashTable and hence technically more efficient as it doesn't lock the complete map while accessing it.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Both provide the thread safety but the actual difference come when talk about performance. CHM gives best performance when no of writer threads are less in number.
Help us improve. Please let us know the company, where you were asked this question :
Ans. ConcurrentHashMaps provide unlimited read operation fro multi threaded environment. For write operation only one thread can change value for particular segment of map.
Help us improve. Please let us know the company, where you were asked this question :