Java HashMap get() Method
Example
Get the value of an entry in a map:
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, String> capitalCities = new HashMap<String, String>();
capitalCities.put("England", "London");
capitalCities.put("Germany", "Berlin");
capitalCities.put("Norway", "Oslo");
capitalCities.put("USA", "Washington DC");
System.out.println(capitalCities.get("England"));
}
}
Definition and Usage
The get()
method returns the value of the entry in the map which has a specified key.
Syntax
public V get(Object key)
V
refers to the data type of the values of the map.
Parameter Values
Parameter | Description |
---|---|
key | Required. Specifies the key of the entry to get the value from. |
Technical Details
Returns: | The value of the entry with the specified key or null if an entry with that key does not exist. |
---|
Related Pages
❮ HashMap Methods