020-004-002
Map Basics: Key-Value Pair Management
Medium
Problem Description
In this problem, you will create a program that registers and retrieves key-value pairs using HashMap<String, Integer>, checks key existence, and displays the result to standard output.
HashMap Basics
HashMap is a class that manages key-value pairs.
Basic Operations
HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 100); // Add key-value pair
map.get("apple"); // Get value by key
map.containsKey("apple"); // Check key existenceLearning Points
- Keys must be unique, values can be duplicated
- get() returns null for non-existent keys
- Use containsKey() to check existence beforehand
Test Cases
※ Output examples follow programming industry standards
Normal case
Input:
3 apple 150 banana 100 orange 200 apple
Expected Output:
150
Normal case
Input:
2 book 500 pen 80 pen
Expected Output:
80
Your Solution
Current Mode:● My Code
99
1
2
3
4
5
6
7
8
9
10
›
⌄
⌄
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Write your code here
sc.close();
}
}
0 B / 5 MB
You have 10 free executions remaining
