Learn How to Use the Java HashMap Data Structure

A HashMap (or a HashTable) is a data structure that allows quick access to data using key-value pairs. The Java HashMap class extends theAbstractMapclass and implements the Map interface, which gives it access to a lot of operations. HashMaps have two type parameters—K and V, where K stores the keys and V stores the values in each HashMap.

The HashMap allows you to map keys to a specific value and store items in random order. In this article, you will learn how to use Java’s HashMap class to store, retrieve, update, and delete data.

4

Creating a Java HashMap

The Java HashMap class has four constructors. The default constructor is the most popular one, it allows you to create an empty HashMap that will have a default capacity of 16.

TheJava classabove uses the default HashMap constructor to create a data structure calledfruitsMap. The fruitsMap object will store data that has an integer key and a string value. However, since the HashMap class implements the Map interface, it can store a null value in either the key field, the value field, or both.

wolf of wall street movie on tubi streaming service.

Storing Data in a HashMap

you may perform several operations using the HashMap class. Theput(K key, V value)method is among its most popular methods, and it allows you to store data using a key-value pair, where each key maps to a value.

The updated class above uses the put() method to add several fruits and their key to the existing fruitsMap data structure. It is important to note that the code above adds the data to the map randomly. This detail will become useful when you start reading the data from the HashMap.

USB-C port on the Google Pixel 8a

Reading Data From a HashMap

There are several ways to read data from a HashMap. The method that you use will depend on how you want the data to look or even the type of data you want to retrieve.

Retrieve the HashMap Object

If you want to retrieve the data as an object, you can simply access the HashMap object, which in the example below is fruitsMap.

Adding the line of code above to the Main class, prints the following output in the console:

mega cloud storage with mega logo on laptop screen.

Though each value was randomly added to the HashMap, from the output you will see that HashMap stores the values in ascending order, based on the key assigned to each value. This is a feature that puts HashMaps ahead of other data structures such as theArray data structurethat only support linear storage.

Retrieve All the Data Individually

If you want to retrieve all the data individually, then you’re able to use theformethod that allows you to traverse through the HashMap and print each value and its corresponding key. This method employs the Map interface that the HashMap class implements.

Adding the method above to your code will print the following output in your console:

Student learning to program Java

Retrieve a Specific Value

The HashMap class has aget()method that takes a key and returns the value mapped to this key.

The line of code above prints the following output in the console:

Updating Data in a HashMap

After you create and populate a HashMap object, you can use thereplace()method to update any item in the Map. The replace() method takes two or three arguments. The first replace() method takes the key associated with an existing item, along with the new value you want to map to it.

Executing the code above prints the following object in the console:

As you can see from the object above, “Grapes” maps to 4, which previously mapped to “Apple”.

The second replace() method takes the key associated with the existing item, the existing item, and the replacement value.

The code above prints the following object in the console:

Deleting Data From a HashMap

You can either delete a specific item from your HashMap using theremove()method or delete all the data using theclear()method. The remove() method takes either one or two arguments. The first remove method takes the key associated with the value you want to remove:

The updated object shows that the item with the key-value pair of 5 and “Berries” is no longer a part of the fruitsMap. The second remove() method takes a key and the value that it maps to.

The code above also prints the following object in the console:

The clear() method takes no arguments and returns void.

Executing the code above prints the following empty object in the console:

Now You Can Perform CRUD Operations on a Java HashMap

HashMaps are one of several popular data structures that you need to know. This article teaches you how to perform CRUD operations on Java HashMaps. The HashMap is a very useful data structure, its main selling point is that it is one of the most efficient data structures, due to its access speed. However, it is also very convenient to use, as it allows random storage.

Data structures are a staple in software engineering. Here are some important data structures every programmer should know.

Sometimes the smallest cleaning habit makes the biggest mess.

Now, I actually finish the books I start.

If an AI can roast you, it can also prep you for emergencies.

The best features aren’t the ones being advertised.

Unlock a world of entertainment possibilities with this clever TV hack.

Technology Explained

PC & Mobile