Java HashMap put() Method

The HashMap.put() method in Java is used to insert key-value pairs into a HashMap. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. put Method Syntax
  3. Examples
    • Adding Entries to a HashMap
    • Updating Values in a HashMap
    • Handling Null Values
    • Real-World Use Case: Storing Employee Information
  4. Conclusion

Introduction

The 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 type K, which represents the key to be inserted or updated.
    • value of type V, 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;

public class PutExample {
    public static void main(String[] args) {
        // Creating a HashMap with String keys and Integer values
        HashMap<String, Integer> people = new HashMap<>();

        // Adding entries to the HashMap
        people.put("Ravi", 25);
        people.put("Priya", 30);
        people.put("Vijay", 35);

        // Printing the HashMap
        System.out.println("HashMap: " + people);
    }
}

Output:

HashMap: {Ravi=25, Priya=30, Vijay=35}

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;

public class UpdateExample {
    public static void main(String[] args) {
        // Creating a HashMap with String keys and Integer values
        HashMap<String, Integer> people = new HashMap<>();

        // Adding entries to the HashMap
        people.put("Ravi", 25);
        people.put("Priya", 30);

        // Updating the value for the key "Ravi"
        Integer oldValue = people.put("Ravi", 28);

        // Printing the old value and the updated HashMap
        System.out.println("Old value for Ravi: " + oldValue);
        System.out.println("Updated HashMap: " + people);
    }
}

Output:

Old value for Ravi: 25
Updated HashMap: {Ravi=28, Priya=30}

Handling Null Values

The put method can handle null keys and values.

Example

import java.util.HashMap;

public class NullValueExample {
    public static void main(String[] args) {
        // Creating a HashMap with String keys and Integer values
        HashMap<String, Integer> people = new HashMap<>();

        // Adding entries with null key and value
        people.put(null, 25);
        people.put("Priya", null);

        // Printing the HashMap
        System.out.println("HashMap with null values: " + people);
    }
}

Output:

HashMap with null values: {null=25, Priya=null}

Real-World Use Case: Storing Employee Information

In a real-world scenario, you might use the put method to store and update employee information in a HashMap.

Example

import java.util.HashMap;

public class EmployeeInfo {
    public static void main(String[] args) {
        // Creating a HashMap with String keys (employee IDs) and String values (employee names)
        HashMap<String, String> employees = new HashMap<>();

        // Adding employee information
        employees.put("E001", "Ravi Kumar");
        employees.put("E002", "Priya Sharma");
        employees.put("E003", "Vijay Singh");

        // Updating employee information
        String oldName = employees.put("E001", "Ravi Kumar Sr.");

        // Printing the old name and the updated employee information
        System.out.println("Old name for E001: " + oldName);
        System.out.println("Updated Employee Information: " + employees);
    }
}

Output:

Old name for E001: Ravi Kumar
Updated Employee Information: {E001=Ravi Kumar Sr., E002=Priya Sharma, E003=Vijay Singh}

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.

Comments