what is Hashmap & Hashtable wirh example?

Answer Posted / venki

1. Hashtable is synchronized which means Hashtable is thread
safe and that can be shared with multiple threads (Suitable
for multi threaded applications), HashMap not synchronized
(Suitable for single threaded applications).
2. Iterator in the Hashtable is fail-fast iterator and throw
ConcurrentModificationException if any other thread modifies
the map by adding or removing elements from the Map, while
Emnumeration in HashMap not.
3. Hashtable is slower than HashMap because of synchronization.
4. HashTable does not allow null for both keys and values,
you will get NullPointerException if you add null. whereas
HashMap allows one null key and any number of null values.
5. Hashtable and HashMap allows duplicate values.
6. Hashtable store objects in insertion order but HashMap
doesn’t store objects in insertion order, to store objects
in insertion order use LinkedHashMap.

Is This Answer Correct ?    0 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is complexity and its types?

530


How do you add an element to a hashset in java?

494


What is the method to declare member of a class static?

539


Define an abstract class with reference to java.

582


What best practices should you follow while writing multithreaded code in java?

549






What are the Main functions of Java?

633


What is hashmap and map?

563


What is the default value of local and global variables?

571


When should I use stringbuffer?

573


Can we define private and protected modifiers for the members in interfaces?

544


Can inner class be public in java?

555


What is the difference between a loader and a compiler?

530


Is empty list java?

527


What is the difference between abstract classes and interfaces?

521


Can we create more than one object singleton class?

585