The HashMap.put()
method in Java is used to insert key-value pairs into a HashMap
.
Table of Contents
- Introduction
put
Method Syntax- Examples
- Adding Entries to a HashMap
- Updating Values in a HashMap
- Handling Null Values
- Real-World Use Case: Storing Country Codes
- Conclusion
Introduction
The HashMap.put()
method is a member of the HashMap
class in Java. It allows you to add or update key-value pairs in a HashMap
. If the key is not already present in the map, the method inserts the new key-value pair and returns null
. If the key is already present, the method updates the value associated with the key and returns the previous value.
put() Method Syntax
The syntax for the put
method is as follows:
public V put(K key, V value)
- The method takes two parameters:
key
of typeK
, which represents the key to be inserted or updated.value
of typeV
, which represents the value to be associated with the key.
- The method returns the previous value associated with the key, or
null
if there was no mapping for the key.
Examples
Adding Entries to a HashMap
The put
method can be used to add key-value pairs to a HashMap
.
Example
import java.util.HashMap;
import java.util.Map;
public class PutExample {
public static void main(String[] args) {
// Creating a Map with String keys and String values
Map<String, String> countries = new HashMap<>();
// Adding entries to the HashMap
countries.put("India", "IN");
countries.put("United States", "US");
countries.put("Canada", "CA");
// Printing the HashMap
System.out.println("HashMap: " + countries);
}
}
Output:
HashMap: {India=IN, United States=US, Canada=CA}
Updating Values in a HashMap
The put
method can be used to update the value associated with an existing key in a HashMap
.
Example
import java.util.HashMap;
import java.util.Map;
public class UpdateExample {
public static void main(String[] args) {
// Creating a Map with String keys and String values
Map<String, String> countries = new HashMap<>();
// Adding entries to the HashMap
countries.put("India", "IN");
countries.put("United States", "US");
// Updating the value for the key "India"
String oldValue = countries.put("India", "IND");
// Printing the old value and the updated HashMap
System.out.println("Old value for India: " + oldValue);
System.out.println("Updated HashMap: " + countries);
}
}
Output:
Old value for India: IN
Updated HashMap: {India=IND, United States=US}
Handling Null Values
The put
method can handle null
keys and values.
Example
import java.util.HashMap;
import java.util.Map;
public class NullValueExample {
public static void main(String[] args) {
// Creating a Map with String keys and String values
Map<String, String> countries = new HashMap<>();
// Adding entries with null key and value
countries.put(null, "Unknown");
countries.put("United States", null);
// Printing the HashMap
System.out.println("HashMap with null values: " + countries);
}
}
Output:
HashMap with null values: {null=Unknown, United States=null}
Real-World Use Case: Storing Country Codes
In a real-world scenario, you might use the put
method to store and update country codes in a HashMap
.
Example
import java.util.HashMap;
import java.util.Map;
public class CountryCodes {
public static void main(String[] args) {
// Creating a Map with String keys (country names) and String values (country codes)
Map<String, String> countryCodes = new HashMap<>();
// Adding country codes
countryCodes.put("India", "IN");
countryCodes.put("United States", "US");
countryCodes.put("Canada", "CA");
// Updating country code for India
String oldCode = countryCodes.put("India", "IND");
// Printing the old code and the updated country codes
System.out.println("Old code for India: " + oldCode);
System.out.println("Updated Country Codes: " + countryCodes);
}
}
Output:
Old code for India: IN
Updated Country Codes: {India=IND, United States=US, Canada=CA}
Conclusion
The HashMap.put()
method in Java provides a way to add or update key-value pairs in a HashMap
. By understanding how to use this method, you can efficiently manage collections of key-value pairs in your Java applications. This method is useful in various scenarios, such as storing and updating data, handling null
values, and managing complex data structures.