In Java, a dictionary is typically created using the Map interface, with the most common implementation being HashMap. Here’s how to create a dictionary in Java:
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// Creating a dictionary (HashMap)
Map
// Adding key-value pairs
dictionary.put(“apple”, 1);
dictionary.put(“banana”, 2);
dictionary.put(“cherry”, 3);
// Accessing values
System.out.println(dictionary.get(“apple”)); // Output: 1
}
}
In this example:
Map
put() is used to insert key-value pairs.
get() retrieves the value associated with a specific key.