0 votes
in JAVA by
What is the difference between Hashtable and HashMap?

2 Answers

0 votes
by

There are many differences between these two classes, some of them are following:

a) Hashtable is a legacy class and present from JDK 1, HashMap was added later.

b) Hashtable is synchronized and slower but HashMap is not synchronized and faster.

c) Hashtable doesn't allow null keys but HashMap allows one null key.

See the answer for more differences between HashMap and Hashtable in Java.

0 votes
by

There are several differences between HashMap and Hashtable in Java:

Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.

Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.

One of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable iteration order (which is insertion order by default), you could easily swap out the HashMap for a LinkedHashMap. This wouldn't be as easy if you were using Hashtable.

Related questions

0 votes
asked Oct 10, 2020 in JAVA by Robindeniel
0 votes
asked Oct 19, 2020 in JAVA by rahuljain1
...