> For the complete documentation index, see [llms.txt](https://codehs.gitbook.io/apjava/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://codehs.gitbook.io/apjava/data-structures/hashmaps.md).

# Hashmaps

&#x20;***Hashmaps*** are helpful tools that allow us to store a key and a value. Unlike Arrays and Arraylists, ***Hashmaps*** allow us to easily look up a value if we know its key. \
&#x20;\
&#x20;With that said, let's say you are creating a piece of software that stores student grades for a class. We want to be able to look up a student's grade by their name, so we will need to use a ***Hashmap***.

## What a Hashmap Looks Like

&#x20;With ***Hashmaps*** we assign a ***key*** and a ***value*** associated with that ***key***. If we are creating a piece of software to store student grades, our ***keys*** are the students' names and the ***value*** is the grade.

This would look like:

| Key             | Value |
| --------------- | ----- |
| Wezley Sherman  | 85    |
| Wade Adams      | 95    |
| Julio Rodriguez | 84    |
| Trevor Forrey   | 100   |
| Ada Lovelace    | 96    |
| Sally Pants     | 98    |
| Sarah Abe       | 86    |

The ***Key*** is the element we use to look up a specific ***Value*** in the map. \
&#x20;The ***Value*** is the result we get when we look up a specific item with a ***Key***. \
&#x20;The ***Key-Value Pair*** is the combination of the ***Key*** and the ***Value*** associated. <br>

It is important to remember that each ***key*** can only have **one** ***value***, and ***key-value pairs*** are **not** ordered.

## Utilizing Hashmaps

### Creating Hashmaps:

In order to create a ***Hashmap*** we use: `Hashmap<key_type, value_type> name = new Hashmap<key_type, value_type>();`. \
&#x20;\
&#x20;If we were to create our grade book ***Hashmap*** we would use:

```java
Hashmap<String, Integer> gradeBook = new Hashmap<String, Integer>();
```

### Adding Items:

To add an item to a ***Hashmap*** we use: `map.put(key, value);` \
&#x20;\
&#x20;If we were to add grades to our grade book ***Hashmap*** we would use:

```java
gradeBook.put("Wezley Sherman", 85);
gradeBook.put("Wade Adams", 95);
gradeBook.put("Julio Rodriguez", 84);
...
```

### Retrieving Items:

To retrieve an item from a ***Hashmap*** we use: `map.get(key);` \
&#x20;\
&#x20;Let's say we wanted to access Trevor Forry's and Ada Lovelace's grades. To do so we would use:

```java
// `adaGrade` would contain `96`
int adaGrade = gradeBook.get("Ada Lovelace");
// `trevGrade` would contain `100`
int trevGrade = gradeBook.get("Trevor Forrey");
```

### Iterating Over a Hashmap:

Now, let's say we want to print out every grade in our grade book. In order to do this, we would have to iterate over our ***Hashmap***. Here is an example of what that would look like:

```java
System.out.println("Class Grades: ");

// For each `key` in gradeBook.keySet()
for(String key: in gradeBook.keySet) 
{
  int grade = gradeBook.get(key);
  System.out.println(key + ": " + grade);
}
```
