A Guide to Java HashMaps

You can access the full course here: Java Foundations

HashMaps

With arrays and ArrayLists, we stored values at specific indexes so the position of the elements is how we identified their values. With HashMaps, we store values at keys rather than at indexes so the position of the elements doesn’t matter. This is beneficial if we want to create a mapping of keys to values. Let’s set up the inventory but with item names and quantities. We can set the keys to be the names and the values to be the quantities. We must first import the HashMap library like this:

import java.util.HashMap;

Then we can replace the inventory with this:

HashMap<String,Integer> inventory = new HashMap<>();

Note how we set the key and value in the <> and that the value is not int. It wants an object of the Integer class but for our purposes it’s exactly the same as a regular int. We have String type keys and int type values. We can then put the values into the HashMap by passing in the key and value like this:

inventory.put("Knife", 2);
inventory.put("Boots", 3);
inventory.put("Axe", 1);

Although this is technically storing 6 values we only consider 3 of those to actually be values and the other 3 are the keys. If we want to retrieve a value, we use the .get() function and pass in the key like this:

int numKnives = inventory.get("Knife");

If we go to put a value into a HashMap but it already exists, it will just replace the value at that key. If we know the key exists, we can use the replace function in the same way but put works just fine:

inventory.replace("Axe", 2);
inventory.put("Axe", 2);

There are some other functions and you can find them in the same way as with ArrayLists.

Transcript

Hello guys. Welcome to our tutorial on HashMaps. Here we’re going to explore a different type of collection, which will be the HashMap. This is essentially a list of key-value pairs. We’re going to start by learning how to create HashMaps. Then how to get and set HashMap elements and then just a couple of the other common operations. Let’s head to the code and get started.

All right, so before we do anything, let’s talk about what HashMaps are and where we use them. So HashMaps essentially represent lists of key-value pairs. So the benefit of using a HashMap instead of let’s say a basic array or an ArrayList, is that we no longer need to worry about the position of the elements.

So with an ArrayList or an array, the position is how we access the individual values themselves and so the position matters. However, we don’t always want that to be the case.

With a key-value pair, we don’t really care about where in the HashMap an item is. We just care about the key under which that value is stored. So we’re essentially storing two pieces of data per value. This means, in order to change a value within a HashMap or to retrieve a value, we have to know what key it’s stored under, okay? So it presents a different kind of a challenge but also helps to solve the problem of forgetting where in the ArrayList items are stored.

So in order to set up a HashMap, the first thing we need to do is import it from java.utils again. So we’re going to import java.utils.HashMap, or actually it’s util.HashMap. Okay, like so and then we can create a HashMap in a somewhat similar fashion to an ArrayList. We do a HashMap, we do the type of HashMap we want and then the name. In this case, we’ll do an inventory again but we’ll take it one step further with the name and the quantity of items, and then we’ll set this equal to a new HashMap, okay? Like so, just like with an ArrayList.

Now, the difference is that we now no longer just pass in the type, maybe a String, but we need the type of the key and the type of the value. So in this case, we’ll do String key, so this’ll be the item names, and we’ll do integer values.

Now, weirdly enough, when we’re declaring the type of integer in a HashMap or even in an ArrayList, we actually don’t do int like this, we do Integer, capital I, but otherwise it represents the same kind of data, okay? So if you’re doing an ArrayList of integers, you do an ArrayList of Integer, not an ArrayList of int. Okay. Cool.

So now let’s add some items to this inventory. We’ll do the inventory and we’ll use the put function. Okay? So the put function takes in a key and a value. In this case, the key needs to be of the type here, so we’ll do, maybe we’ll do like a Knife again and then the value. Maybe we have a couple of knives. Okay, just like before. And we’ll just add in a few more items because why not? We’ll also add in maybe a couple pairs of boots. We have three pairs of boots, perhaps. Maybe we have like one Axe or something and so on and so forth.

So the order of these items, the order in which we put them really doesn’t matter because we access the values, two, three and one based on the key that they’re stored under.

Now, in order to get something, we’re gonna do the inventory. And we’re going to use the get function. And this way, we need to input the key and this will retrieve us the value. So maybe we want whatever value stored under Knife. We wanna see how many knives we have, and this would return us, in this case, an integer, okay? Because the values are integers. So we would do int knife is going to be equal. You know what? We would actually do number of Knives is going to be equal to the result here.

Now, one thing I should point out before moving on is that the put function will insert a key and then assign the value. However, if it finds the key that already exists, for example, if I did this, put Axe of two, it’s going to find this key and then it’s just gonna replace whatever value it finds, okay? So if the key does not exist, it inserts the key and assigns a value. If the key does exist, then it just replaces the value.

Alternatively, if you know that the key exists, you can just do the inventory.replace, okay? And then we can pass in either the key and the oldValue, newValue, or I think you can actually do the key and the newValue. So in this case, it would be the Axe and then the value of two, all right? Now that’s all we’re gonna cover in this tutorial.

Similar to the ArrayList, there is a plethora of functions that you can choose from. So you can open up your inventory dot and you can see that there are a lot of functions to explore. So feel free to explore some of them. Don’t worry, you really don’t need to check out all of them. Perhaps just the specific kind of key and value pairs. For example, you need a set of just the keys or just the values. That should be good enough. When you’re ready to move on, we’ll switch topics and start talking about implementing logic and control flow with if statements. So stay tuned for that. Thanks for watching, see you guys in the next one.

Interested in continuing? Check out the full Java Foundations course, which is part of our One-Hour Coder Academy.