HashMap Basics in Java: Explanation & Practice

Store key-value pairs

Problem summary

Create a HashMap to store student names and their scores.

Starter code

import java.util.*;

public class Main {
    public static void main(String[] args) {
        // Create HashMap<String, Integer>
        // Add: Alice=95, Bob=87, Carol=92
        // Print Bob's score
    }
}

Expected output and test cases

  • Get value by key
    Bob's score: 87

Hints

  1. HashMap<K, V> for key-value storage
  2. put(key, value) to add
  3. get(key) to retrieve

Related Collections exercises

Practice all Collections exercises · Run this idea in the Java compiler