top of page

HashMap

In this section, we'll discuss HashMaps. HashMaps are unlike any other data structure we've learned so far. Instead of storing a set of elements, a HashMap stores a set of pairs, equipped with a set of keys, and their corresponding values.

HashMap

We'll want to use HashMap when there are sets of corresponding data. 

Syntax:

HashMap<Object1, Object2> map = new HashMap<>();

This is how we declare HashMaps in Java. We declared a HashMap called map, which maps Object1 to Object2. What does this mean? This means that our Object1, which may be a String, Integer, etc, acts as a key that maps to Object2, which is also just a String, Integer, etc.​

There are many useful methods that we can do with HashMaps in Java. 

Method 1:

map.put(key, value)

This method will add the pair (key, value) to map. This means that value will correspond to the key. For example, if we had map = <(1, "hello"), (2, "goodbye")>, and we write map.put(3, "test"), our result will be

map = <(1, "hello"), (2, "goodbye"), (3, "test")>

Method 2:

map.remove(key, value)

This method will remove whatever the pair is in the parenthesis. Take the example where we had 

map = <(1, "hello"), (2, "goodbye")>. Then after we take map.remove(2, "goodbye"), we will get map = <(1, "hello")>. These 2 methods will be useful when we want to move items in and out of the map.

Method 3:

map.get(key)

This method will return the value associated with the key specified in the parenthesis. For example, if we have 

map = <(1, "hello"), (2, "goodbye")>, and we take map.get(1), this will return "hello". This is because the value associated with the key of 1 is "hello".

More Methods:

There are many other more specific methods we can use. For more details, visit the official java library by Oracle!

Iteration:  As with any data structure, iteration is always important. How do we iterate through a Set?

Example 1:

                                                 {

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

                                                      map.put("hello", 10);

                                                      map.put("goodbye", 20);

                                                      map.put("filler", 30);

                                                      for (Map.Entry<String, Integer> e : map.entrySet())

                                                      System.out.println("Key: " + e.getKey()

                                                                      + " Value: " + e.getValue());

                                                 }

When we want to iterate over a Hashmap, we can use an enhanced for loop, or "for-each" loop to do so. This will allow us to access all of the elements in the map. In this example, we iterate through all entries of the map, and print the key and value of each entry. See if you can decipher the code! If you get stuck, the library will be very helpful.

Exercise(s):

1. Make a HashMap with 10 elements and copy the contents into another HashMap using loops.

2. Be Creative! Make a HashMap that corresponds to your daily life. Practice some of the methods we learned in this unit on your map.

bottom of page