Java Hashmap Get
## Java HashMap get() Method
The `get()` method of the `HashMap` class in Java is used to retrieve the value associated with a specified key. If the map contains no mapping for the key, this method returns `null`.
---
## Syntax
The syntax of the `get()` method is as follows:
```java
public V get(Object key)
```
*Note: Here, `V` represents the type of mapped values in the `HashMap`.*
### Parameters
* **`key`**: The key whose associated value is to be returned.
### Return Value
* Returns the **value** to which the specified key is mapped.
* Returns **`null`** if this map contains no mapping for the key.
---
## Code Examples
### Example 1: Retrieving a String Value Using an Integer Key
The following example demonstrates how to create a `HashMap` with `Integer` keys and `String` values, and retrieve a value using the `get()` method.
```java
import java.util.HashMap;
class Main {
public static void main(String[] args) {
// Create a HashMap
HashMap sites = new HashMap<>();
// Add elements to the HashMap
sites.put(1, "Google");
sites.put(2, "YouTip");
sites.put(3, "Taobao");
System.out.println("sites HashMap: " + sites);
// Retrieve the value associated with key 1
String value = sites.get(1);
System.out.println("Value associated with key 1: " + value);
}
}
```
**Output:**
```text
sites HashMap: {1=Google, 2=YouTip, 3=Taobao}
Value associated with key 1: Google
```
---
### Example 2: Retrieving an Integer Value Using a String Key
In this example, we use a `String` key to retrieve an `Integer` value from the `HashMap`.
```java
import java.util.HashMap;
class Main {
public static void main(String[] args) {
// Create a HashMap
HashMap primeNumbers = new HashMap<>();
// Add mappings to the HashMap
primeNumbers.put("Two", 2);
primeNumbers.put("Three", 3);
primeNumbers.put("Five", 5);
System.out.println("HashMap: " + primeNumbers);
// Retrieve the value associated with key "Three"
int value = primeNumbers.get("Three");
System.out.println("Value associated with key \"Three\": " + value);
}
}
```
**Output:**
```text
HashMap: {Five=5, Two=2, Three=3}
Value associated with key "Three": 3
```
---
## Important Considerations
### 1. Handling `null` Return Values
A return value of `null` does not *necessarily* mean that the map contains no mapping for the key. Because `HashMap` supports `null` values, it is possible that the key is explicitly mapped to `null`.
If you need to distinguish between a key that does not exist and a key that is explicitly mapped to `null`, you should use the `containsKey()` method:
```java
if (map.containsKey(key)) {
// The key exists in the map (even if its value is null)
Object value = map.get(key);
} else {
// The key does not exist in the map
}
```
### 2. Alternative: `getOrDefault()`
If you want to avoid dealing with `null` values when a key is missing, you can use the `getOrDefault(Object key, V defaultValue)` method. This allows you to specify a fallback value if the requested key is not found:
```java
// Returns "Unknown" if key 4 does not exist
String site = sites.getOrDefault(4, "Unknown");
```
YouTip