Java also provides us a structure to store data as key/value pairs. Java calls
these objects hashmaps (or maps, more generally), and they are
provided by the
Considering the gradebook example, we can improve our program using a map. We’ll store the students’ grades along with their names in the same data structure. The names will be the keys, and the grades will be the values.
As with the other collection structures, in Java we must specify the types of the objects we’ll be storing when we declare a variable or parameter to be a map. This means specifying both key and value data types, which are allowed to be different types for a given map.
Notice how a
students is declared on line 11:
<String, Double> defines the data types for this map’s
<key, value> pairs. Like the
ArrayList, when we call the
constructor on the right side of the assignment, we don’t need to specify
We can add a new item with a
.put() method, specifying both key and
And while we don’t do so in this example, we may also access
elements using the
get method. If we had a key/value pair of
"jesse"/4.0 in the
students map, we could access the grade with:
Double jesseGrade = students.get("jesse");
Variables may be used to access elements:
Looping through a map is slightly more complex than it is for ordered lists.
Let’s look at the
for-each loop from this example:
The iterator variable,
student, is of type
Map.Entry<String, Double>. The class
Map.Entry is specifically
constructed to be used in this fashion, to represent key/value pairs
within HashMaps. Each
Map.Entry object has a
getKey method and a
getValue method, which represent (surprisingly enough!), the key and
value of the map item.
If you only need to access the key of each item in a map, you can construct a simpler loop:
A similar structure applies if you only need the values, using
Let’s collect some
HashMap methods as we have for
ArrayList. As we
ArrayLists, this is by no means a comprehensive list. For full
details on all properties and methods available, see the reference section
below for official documentation on the
For the purposes of this table, we’ll create a map to hold our solar system’s planets and the number of moons associated with each.
|Returns the number of items in the map, as an |
|Returns a collection containing all keys in the map. This collection may be used in a |
|Returns a collection containing all values in the map. This collection may be used in a |
|Add a key/value pair to a map.|
|Returns a boolean indicating whether or not the map contains a given key.|
|Returns a boolean indicating whether or not the map contains a given value.|
We have only brushed the surface of how arrays,
ArrayLists, and maps work.
We leave it to you to refer to the official documentation linked below for more
details. You’ll certainly be using
ArrayLists and maps in more ways than
those covered in this lesson, but with the knowledge you have now, you
should be able to use Java collections and learn new uses as you go.
Check Your Understanding
What is the method to return the key names?