The hash map is used whenever data is stored as key-value pairs, where values can be added, retrieved, and deleted using keys.

Using a hash map requires the import java.util.HashMap; statement at the beginning of the class.

Two type parameters are required when creating a hash map - the type of the key and the type of the value added.

If the keys of the hash map are of type string, and the values of type integer, the hash map is created with the following statement HashMap<String, Integer> hashmap = new HashMap<>();

Adding to the hash map is done through the put(*key*, *value*) method that has two parameters, one for the key, the other for the value.

Retrieving from a hash map happens with the help of the get(*key*) method that is passed the key as a parameter and returns a value.

Hash Map Keys Correspond to a Single Value at Most

If a new key-value pair is added to the hash map, but the key has already been associated with some other value stored in the hash map, the old value will vanish from the hash map.

When Should Hash Maps Be Used?

The hash map is implemented internally in such a way that searching by a key is very fast.

The hash map generates a "hash value" from the key, i.e. a piece of code, which is used to store the value of a specific location. When a key is used to retrieve information from a hash map, this particular code identifies the location where the value associated with the key is.

Java's System.nanoTime() method returns the time of the computer in nanoseconds.

The hash maps also have no internal order, and it is not possible to search the hash map based on the indexes.

Hash maps work well when we know exactly what we are looking for.

Typically, hash maps and lists are used together. The hash map provides quick access to a specific key or keys, while the list is used, for instance, to maintain order.

Hash Map as an Instance Variable

The toLowerCase() method creates a new string with all letters converted to lowercase.

The trim() method, on the other hand, creates a new string where empty characters such as spaces at the beginning and end have been removed.

The containsKey method of the hash map is being used above to check for the existence of a key.

Repetitive code is often not noticed until it has already been written, which means that it almost always makes its way into the code. There's nothing wrong with that - the important thing is that the code is cleaned up so that places that require tidying up are noticed.