Java IdentityHashMap containsValue() Method

The IdentityHashMap.containsValue() method in Java is used to check if the map maps one or more keys to the specified value. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality. We will also cover a real-world use case to show how IdentityHashMap.containsValue() can be used effectively.

Table of Contents

  1. Introduction
  2. containsValue Method Syntax
  3. Examples
    • Basic Usage of containsValue Method
    • Checking for Non-Existent Values
  4. Real-World Use Case
    • Example: Managing User Roles in a System
  5. Conclusion

Introduction

The IdentityHashMap.containsValue() method is a member of the IdentityHashMap class in Java. This class uses reference equality (==) instead of object equality (equals()) when comparing keys. The containsValue method checks if the map maps one or more keys to the specified value.

containsValue() Method Syntax

The syntax for the containsValue method is as follows:

public boolean containsValue(Object value)
  • Parameters:
    • value: The value whose presence in this map is to be tested.
  • Returns: true if this map maps one or more keys to the specified value.

Examples

Basic Usage of containsValue Method

The containsValue method can be used to check if a specified value is present in an IdentityHashMap.

Example

import java.util.IdentityHashMap;

public class IdentityHashMapContainsValueExample {
    public static void main(String[] args) {
        // Creating an IdentityHashMap
        IdentityHashMap<String, Integer> map = new IdentityHashMap<>();

        // Adding key-value pairs to the IdentityHashMap
        map.put("Ravi", 25);
        map.put("Priya", 30);
        map.put("Vijay", 35);

        // Checking if specific values are present in the IdentityHashMap
        boolean contains25 = map.containsValue(25);
        boolean contains40 = map.containsValue(40);

        // Printing the results
        System.out.println("Contains value 25: " + contains25);
        System.out.println("Contains value 40: " + contains40);
    }
}

Output:

Contains value 25: true
Contains value 40: false

Checking for Non-Existent Values

If the specified value is not found in the IdentityHashMap, the containsValue method returns false.

Example

import java.util.IdentityHashMap;

public class IdentityHashMapContainsNonExistentValueExample {
    public static void main(String[] args) {
        // Creating an IdentityHashMap
        IdentityHashMap<String, Integer> map = new IdentityHashMap<>();

        // Adding key-value pairs to the IdentityHashMap
        map.put("Ravi", 25);
        map.put("Priya", 30);

        // Checking if a non-existent value is present in the IdentityHashMap
        boolean contains35 = map.containsValue(35);

        // Printing the result
        System.out.println("Contains value 35: " + contains35);
    }
}

Output:

Contains value 35: false

Real-World Use Case

Example: Managing User Roles in a System

A common real-world use case for IdentityHashMap.containsValue() is managing user roles in a system to check if a specific role is assigned to any user.

Example

import java.util.IdentityHashMap;

public class UserRoleManager {
    static class User {
        private String name;

        public User(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) {
        // Creating user objects
        User user1 = new User("Ravi");
        User user2 = new User("Priya");
        User user3 = new User("Vijay");

        // Creating an IdentityHashMap to manage user roles
        IdentityHashMap<User, String> roleMap = new IdentityHashMap<>();
        roleMap.put(user1, "Admin");
        roleMap.put(user2, "User");

        // Checking if specific roles are assigned
        boolean containsAdmin = roleMap.containsValue("Admin");
        boolean containsManager = roleMap.containsValue("Manager");

        // Printing the results
        System.out.println("Contains role 'Admin': " + containsAdmin);
        System.out.println("Contains role 'Manager': " + containsManager);
    }
}

Output:

Contains role 'Admin': true
Contains role 'Manager': false

In this example, IdentityHashMap.containsValue() is used to check if specific roles are assigned to any user, where roles are identified by reference equality, making it suitable for scenarios where unique object references are crucial.

Conclusion

The IdentityHashMap.containsValue() method in Java provides a way to check if the map maps one or more keys to the specified value. By understanding how to use this method, you can efficiently manage collections of key-value pairs where reference equality is required. This method allows you to utilize the power of IdentityHashMap for various scenarios, making it a versatile tool for managing collections of key-value pairs based on reference equality.

Comments