xxxxxxxxxx
//Import Hashmap
import java.util.HashMap;
HashMap<String, String> dir = new HashMap<String, String>();
//Add key, values
dir.put("hi", "hello");
dir.put("wow", "amazing");
//print value for hi.
System.out.println(dir.get("hi");
xxxxxxxxxx
// Java code to illustrate the get() method
import java.util.*;
public class Map_Demo {
public static void main(String[] args)
{
// Creating an empty Map
Map<Integer, String> map = new HashMap<Integer, String>();
// Mapping string values to int keys
map.put(10, "Geeks");
map.put(15, "4");
map.put(20, "Geeks");
map.put(25, "Welcomes");
map.put(30, "You");
// Displaying the Map
System.out.println("Initial Mappings are: " + map);
// Getting the value of 25
System.out.println("The Value is: " + map.get(25));
// Getting the value of 10
System.out.println("The Value is: " + map.get(10));
}
}
xxxxxxxxxx
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
// Create a HashMap
HashMap<String, Integer> hashMap = new HashMap<>();
// Add some key-value pairs to the HashMap
hashMap.put("key1", 100);
hashMap.put("key2", 200);
hashMap.put("key3", 300);
// Retrieve a value from the HashMap using a specific key
String key = "key2";
int value = hashMap.get(key);
// Print the retrieved value
System.out.println("The value for key '" + key + "' is: " + value);
}
}