Java LinkedHashMap entrySet() Method

The LinkedHashMap.entrySet() method in Java is used to obtain a set view of the mappings contained in the LinkedHashMap.

Table of Contents

  1. Introduction
  2. entrySet Method Syntax
  3. Examples
    • Iterating Over Entries in a LinkedHashMap
    • Modifying Entries Using Entry Set
  4. Real-World Use Case
    • Example: Displaying User Profiles
  5. Conclusion

Introduction

The LinkedHashMap.entrySet() method is a member of the LinkedHashMap class in Java. It returns a Set view of the mappings contained in the map. This Set is backed by the LinkedHashMap, so changes to the map are reflected in the set, and vice-versa. This method is useful when you need to iterate over the entries in the map or perform bulk operations on the mappings.

entrySet() Method Syntax

The syntax for the entrySet method is as follows:

public Set<Map.Entry<K, V>> entrySet()
  • The method does not take any parameters.
  • The method returns a set view of the mappings contained in the map.

Examples

Iterating Over Entries in a LinkedHashMap

The entrySet method can be used to iterate over the entries in a LinkedHashMap.

Example

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

public class EntrySetExample {
    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);

        // Getting the entry set
        Set<Map.Entry<String, Integer>> entrySet = people.entrySet();

        // Iterating over the entry set
        for (Map.Entry<String, Integer> entry : entrySet) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

Output:

Ravi: 25
Priya: 30
Vijay: 35

Modifying Entries Using Entry Set

You can also modify the values in the LinkedHashMap using the entry set.

Example

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

public class ModifyEntrySetExample {
    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);

        // Getting the entry set
        Set<Map.Entry<String, Integer>> entrySet = people.entrySet();

        // Modifying the entries in the entry set
        for (Map.Entry<String, Integer> entry : entrySet) {
            if (entry.getKey().equals("Ravi")) {
                entry.setValue(26);
            }
        }

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

Output:

Modified LinkedHashMap: {Ravi=26, Priya=30, Vijay=35}

Real-World Use Case

Example: Displaying User Profiles

A common real-world use case for LinkedHashMap.entrySet() is displaying user profiles stored in a LinkedHashMap. For example, let's consider a scenario where user profiles are stored with usernames as keys and profile information as values.

Example

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

public class UserProfileDisplay {
    public static void main(String[] args) {
        // Creating a LinkedHashMap to store user profiles
        LinkedHashMap<String, String> userProfiles = new LinkedHashMap<>();

        // Adding user profiles to the LinkedHashMap
        userProfiles.put("Ravi", "Age: 25, Location: Mumbai");
        userProfiles.put("Priya", "Age: 30, Location: Delhi");
        userProfiles.put("Vijay", "Age: 35, Location: Bangalore");

        // Getting the entry set
        Set<Map.Entry<String, String>> entrySet = userProfiles.entrySet();

        // Displaying the user profiles
        System.out.println("User Profiles:");
        for (Map.Entry<String, String> entry : entrySet) {
            System.out.println(entry.getKey() + " -> " + entry.getValue());
        }
    }
}

Output:

User Profiles:
Ravi -> Age: 25, Location: Mumbai
Priya -> Age: 30, Location: Delhi
Vijay -> Age: 35, Location: Bangalore

In this example, LinkedHashMap.entrySet() is used to display user profiles stored in a LinkedHashMap, demonstrating how to iterate over and access entries in the map.

Conclusion

The LinkedHashMap.entrySet() method in Java provides a way to obtain a set view of the mappings contained in the LinkedHashMap. By understanding how to use this method, you can efficiently manage and manipulate collections of key-value pairs in your Java applications. The method allows you to iterate over entries, modify values, and perform bulk operations on the mappings, making it a versatile tool for data management.

Comments