🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
- Introduction
containsValueMethod Syntax- Examples
- Basic Usage of
containsValueMethod - Checking for Non-Existent Values
- Basic Usage of
- Real-World Use Case
- Example: Managing User Roles in a System
- 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:
trueif 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment