AP Computer Science in Java
  • Introduction
  • Introduction to Programming in Java with Karel the Dog
    • Introduction to Programming with Karel
    • More Basic Karel
    • Java Programs and the Run Method
    • Karel Can't Turn Right
    • Methods in Karel
    • Top Down Design and Decomposition in Karel
    • Commenting Your Code
    • SuperKarel
    • For Loops
    • While Loops in Karel
    • If Statements
    • If/Else Statements
    • Control Structures Example
    • How To Indent Your Code
  • Basic Java
    • Printing in Java
    • Variables and Types
    • User Input
    • Arithmetic Expressions
    • Casting
    • Booleans
    • Logical Operators
    • Comparison Operators
    • For Loops
    • While Loops
    • If Statements
    • Loop-and-a-Half
    • Short-Circuit Evaluation
    • De Morgan's Laws
    • Strings
  • Methods
    • Java Methods
    • Methods and Parameters
    • Methods and Return Values
    • Javadoc and More Methods
    • Strings Methods
    • Strings and Characters
    • Exceptions
    • String Processing
  • Classes and Object-Oriented Programming
    • Introduction To Classes and Objects
    • Classes vs. Objects
    • Using a Class as a Client
    • Writing Classes
    • Writing Classes and Instance Methods
    • Getter and Setter Methods
    • Class Methods and Class Variables
    • Method Overloading
    • Local Variables and Scope
    • Key Terms for Classes
    • Objects vs Primitives
    • Inheritance
    • Class Design and Abstract Classes
    • Polymorphism
    • Interfaces
  • Data Structures
    • What Are Data Structures?
    • Introduction to Arrays
    • Using Arrays
    • ArrayList Methods
    • Arrays vs ArrayLists
    • 2D Arrays (Matrices or Grids)
    • Hashmaps
  • Algorithms and Recursion
    • What is an Algorithm?
    • Pseudocode
    • Linear Search
    • Binary Search
    • Selection Sort
    • Insertion Sort
    • Advanced: Recursion
    • Mergesort
Powered by GitBook
On this page
  • What a Hashmap Looks Like
  • Utilizing Hashmaps
  • Creating Hashmaps:
  • Adding Items:
  • Retrieving Items:
  • Iterating Over a Hashmap:

Was this helpful?

  1. Data Structures

Hashmaps

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. 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

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. The Value is the result we get when we look up a specific item with a Key. The Key-Value Pair is the combination of the Key and the Value associated.

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>();. If we were to create our grade book Hashmap we would use:

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

Adding Items:

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

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); Let's say we wanted to access Trevor Forry's and Ada Lovelace's grades. To do so we would use:

// `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:

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);
}
Previous2D Arrays (Matrices or Grids)NextAlgorithms and Recursion

Last updated 5 years ago

Was this helpful?