import java.util.HashMap; import java.util.Map; public class HashMapExample { public static void main(String[] args) { Mapmap = new HashMap<>(); map.put("John", 25); map.put("Jane", 30); map.put("Bob", 35); System.out.println(map.get("John")); // Output: 25 } }
import java.util.Map; import java.util.TreeMap; public class TreeMapExample { public static void main(String[] args) { MapIn both examples, we first import the required classes from the java.util package. We then create a Map object and add key-value pairs using the put() method. Finally, we retrieve the value associated with a key using the get() method. The package library used in these examples is java.util.map = new TreeMap<>(); map.put("John", 25); map.put("Jane", 30); map.put("Bob", 35); System.out.println(map.get("Bob")); // Output: 35 } }