RivoCode

Java Fundamentals · Arrays & Collections · Lesson 18 of 22

HashMap: Key-Value Pairs

Concept

HashMap: Key-Value Pairs

A HashMap stores key-value pairs, similar to a dictionary, letting you look up a value quickly by its key instead of by position.

By the end of this lesson
  • Explain the core idea behind HashMap: Key-Value Pairs.
  • Predict output before running a change.
  • Test one realistic and one unusual input.
  • Use the result to make the next decision.
How to study this page
  1. 1. Read one concept.
  2. 2. Change the example.
  3. 3. Run, compare, and explain.
  4. 4. Complete the challenge below.
ExampleRunnable
import java.util.HashMap;

HashMap<String, Integer> ages = new HashMap<>();
ages.put("Alex", 20);
System.out.println(ages.get("Alex"));
Try it Yourself »
Self-check before continuing

Without looking at the example, describe what changes when you modify one input in HashMap: Key-Value Pairs. Then reopen the editor and prove your explanation with a small test.

You are ready to continue when you can predict, test, and explain the result.

Your turn

Create a HashMap mapping course names to their difficulty level, then print one value.

Loading editor…

Console

Output will appear here...