# Hashmaps

Hashmaps are one of the most useful data structures in coding. It serves a very specific purpose in that it provides you a way of specifying a "key" to store data. And you can then use the "key" to retrieve data quickly. And why would you want to store data and look it up using a key? Imagine you have a list of 100 orders and you're looking for orders by a specific customer. If they were stored in an array, you would have to search each order and check if the order is from the customer you're looking for. You might have to search all 100 orders depending on where in the array it's located. Using a hashmap, you can store each order with the customer's name as the key. And now instead of checking every order, you can just use the key to find the order you're looking for. So let's look at a basic hashmap:

Copy code
      
    

So we have a hashmap that contains the ages of 3 individuals: mochi, momo and fluffy (just 3 of the many names I call my dog). The basic syntax/structure of a hashmap is to have a key followed by : and then the value. And then just like an array, we have , separating values. Our first key/value is mochi with a value of 10.

Next, let's see how we access the data using a key once it's been stored:

Copy code
      
    

This should look fairly familiar to how we accessed data in an array. But instead of the position/index in an array, we specify the key that was used to store the data in the first place. And next, let's look at how we update values in a hashmap:

Copy code
      
    

And again, this should look similar to how we updated values in an array. We simply specify the key and then use the = to update the value for the specified key. You might notice similarities between an array and a hashmap and wonder why do we need two data structures. While there are some similarities between arrays and hashmaps, they are quite different and have very distinct uses. We'll save this topic for another lesson and explain the different scenarios where an array would be a better data structure and vice versa.

Next: Classes

Join our community

Your sign-up could not be saved. Please try again.
Your sign-up was successful.

Sign-up and get tips to becoming a software engineer.