Java LinkedHashMap put() Method

The LinkedHashMap.put() method in Java is used to insert key-value pairs into a LinkedHashMap.

Table of Contents

  1. Introduction
  2. put Method Syntax
  3. Examples
    • Adding Entries to a LinkedHashMap
    • Handling Duplicate Keys
  4. Real-World Use Case
    • Example: Tracking Order of User Actions
  5. Conclusion

Introduction

The LinkedHashMap.put() method is a member of the LinkedHashMap class in Java. It allows you to add or update key-value pairs in a LinkedHashMap. 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. 

The LinkedHashMap maintains a doubly-linked list running through all of its entries, which defines the iteration ordering, typically the order in which keys were inserted into the map.

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 LinkedHashMap

The put method can be used to add key-value pairs to a LinkedHashMap.

Example

import java.util.LinkedHashMap;

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

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

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

Output:

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

Handling Duplicate Keys

The put method returns the previous value if the key is already present in the LinkedHashMap.

Example

import java.util.LinkedHashMap;

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

        // Adding entries to the LinkedHashMap
        Integer previousValue1 = people.put("Ravi", 25);
        Integer previousValue2 = people.put("Priya", 30);
        Integer previousValue3 = people.put("Ravi", 40); // Updating the value for the key "Ravi"

        // Printing the results of adding entries
        System.out.println("Previous value for Ravi: " + previousValue1);
        System.out.println("Previous value for Priya: " + previousValue2);
        System.out.println("Previous value for Ravi after update: " + previousValue3);

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

Output:

Previous value for Ravi: null
Previous value for Priya: null
Previous value for Ravi after update: 25
LinkedHashMap: {Ravi=40, Priya=30}

Real-World Use Case

Example: Tracking Order of User Actions

A common real-world use case for LinkedHashMap is tracking the order of user actions in an application. For example, let's consider a simple web application where users perform various actions, and we want to keep track of the order in which these actions were performed.

Example

import java.util.LinkedHashMap;
import java.util.Map;

public class UserActionTracker {
    public static void main(String[] args) {
        // Creating a LinkedHashMap to track user actions
        LinkedHashMap<String, String> userActions = new LinkedHashMap<>();

        // Recording actions
        userActions.put("Login", "10:00 AM");
        userActions.put("ViewProfile", "10:05 AM");
        userActions.put("UpdateProfile", "10:10 AM");
        userActions.put("Logout", "10:15 AM");

        // Printing the order of user actions
        System.out.println("User Actions: ");
        for (Map.Entry<String, String> entry : userActions.entrySet()) {
            System.out.println(entry.getKey() + " at " + entry.getValue());
        }
    }
}

Output:

User Actions:
Login at 10:00 AM
ViewProfile at 10:05 AM
UpdateProfile at 10:10 AM
Logout at 10:15 AM

In this example, LinkedHashMap is used to maintain the order in which user actions were performed, making it easy to retrieve and display the sequence of actions.

Conclusion

The LinkedHashMap.put() method in Java provides a way to add or update key-value pairs in a LinkedHashMap. By understanding how to use this method, you can efficiently manage collections of key-value pairs in your Java applications while maintaining the insertion order. The method allows you to handle both the insertion of new pairs and the updating of existing pairs, making it a versatile tool for data management.

Comments