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
- HashMap<K, V> for key-value storage
- put(key, value) to add
- get(key) to retrieve
Related Collections exercises
- Practice ArrayList Basics in Java
- Practice HashSet Basics in Java
- Practice List Iteration Methods in Java
Practice all Collections exercises · Run this idea in the Java compiler