The Map.get() method in Java belongs to the java.util package and is used to fetch the value associated with a specified key from a map. If the map contains the key, the method returns the corresponding value. If not, the method returns null.
Here's an example that illustrates the use of Map.get() in Java:
import java.util.HashMap; import java.util.Map;
public class MapExample { public static void main(String[] args) { Map map = new HashMap<>(); map.put("John", 25); map.put("Jane", 30);
System.out.println("Age of John: " + map.get("John")); // Output: Age of John: 25 System.out.println("Age of Lily: " + map.get("Lily")); // Output: Age of Lily: null } }
In the above example, we create a HashMap that maps String keys to Integer values. We add two key-value pairs to the map using the put() method. We then use the get() method to fetch the age of "John" and "Lily" from the map. Since the map contains a key "John", the get() method returns the corresponding value 25. However, since the map does not contain the key "Lily", the get() method returns null.
In conclusion, the Map.get() method is a useful method to retrieve values from a map in Java. It is part of the java.util package and can be used with any map that implements the Map interface.
Java Map.get - 30 examples found. These are the top rated real world Java examples of java.io.Map.get extracted from open source projects. You can rate examples to help us improve the quality of examples.