Source Code:
(back to article)
import java.util.HashMap; public class Main { public static void main(String[] args) { // Create a HashMap HashMap<String, Integer> map = new HashMap<>(); // Add key-value pairs to the HashMap map.put("Key1", 1); map.put("Key2", 2); map.put("Key3", 3); // Access values based on the keys int value1 = map.get("Key1"); int value2 = map.get("Key2"); int value3 = map.get("Key3"); // Print the values System.out.println(value1); System.out.println(value2); System.out.println(value3); } }
Result:
Report an issue