Differences between HashMap and Hashtable in Java

What are the differences between two similar Java classes? Check this article to find out
15 August 2017   1493

What is HashMap? 

The HashMap class uses a hash table to store the card, providing a fast execution time for the get() and put() queries for large sets. The class implements the Map interface (data storage in the form of key / value pairs). Keys and values can be of any type, including null. All keys must necessarily be unique, and the values can be repeated. This implementation does not guarantee the order of the elements.

Example:

// K - Key, V - Value
class HashMap<K, V>

You can declare it as follows:

Map<String, Integer> hashMap = new HashMap<String, Integer>();

Map<String, String> hashMap = new HashMap<String, String>();

What is Hashtable? 

Hashtable class implements a hashtable, which maps keys to values. It inherits Dictionary class and implements the Map interface.

Main Hashtable features:

  • A Hashtable is an array of list. Each list is known as a bucket. The position of bucket is identified by calling the hashcode() method. A Hashtable contains values based on the key.
  • It contains only unique elements.
  • It may have not have any null key or value.
  • It is synchronized.

Differences between HashMap and Hashtable

After our research, we've found out that there are 3 main differences:

  1. Hashtable is synchronized, and HashMap is not. Due to this fact, HashMap is better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.

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

  3. 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.

Tiobe February 2019 Index Released

Groove language is in top 20 again; it's popularity increased due to Gradle; Java is 1st
11 February 2019   326

In February, search engine statistics showed that the dynamically typed Groovy language is gaining popularity again. As analysts of TIOBE noted, it was already in the top twenty of the TIOBE index in 2016 on the wave of interest in the Jenkins system for continuous integration. Now it is pushed forward by the Gradle assembly automation system.

TIOBE Februry 2019 Index
TIOBE Februry 2019 Index

More analysts noticed that the language Hack this month entered the top 50, and TypeScript left it (in their opinion, temporarily). Since February last year, Python and C ++ swapped places, and again topped the list with Java and C.