🎓 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
HashMap.containsKey() method in Java is used to check if a specific key is present in a HashMap. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
containsKeyMethod Syntax- Examples
- Checking for the Presence of Keys in a HashMap
- Real-World Use Case: Verifying Employee IDs
- Conclusion
Introduction
The containsKey() method is a member of the HashMap class in Java. It allows you to check if a specific key exists in the HashMap. This can be useful when you need to verify the existence of a key before performing operations that depend on the presence of that key.
containsKey() Method Syntax
The syntax for the containsKey method is as follows:
public boolean containsKey(Object key)
- The method takes a single parameter
keyof typeObject, which represents the key to be checked for presence in theHashMap. - The method returns a boolean value:
trueif theHashMapcontains a mapping for the specified key.falseif theHashMapdoes not contain a mapping for the specified key.
Examples
Checking for the Presence of Keys in a HashMap
The containsKey method can be used to check if a specific key is present in a HashMap.
Example
import java.util.HashMap;
public class ContainsKeyExample {
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);
// Checking for the presence of keys in the HashMap
boolean containsRavi = people.containsKey("Ravi");
boolean containsAmit = people.containsKey("Amit");
// Printing the results
System.out.println("HashMap contains key 'Ravi': " + containsRavi);
System.out.println("HashMap contains key 'Amit': " + containsAmit);
}
}
Output:
HashMap contains key 'Ravi': true
HashMap contains key 'Amit': false
Real-World Use Case: Verifying Employee IDs
In a real-world scenario, you might use the containsKey method to verify if an employee ID exists in a company's employee database before performing operations such as updating employee details or processing payroll.
Example
import java.util.HashMap;
public class EmployeeIDVerification {
public static void main(String[] args) {
// Creating a HashMap with String keys (employee IDs) and String values (employee names)
HashMap<String, String> employeeDatabase = new HashMap<>();
// Adding entries to the HashMap
employeeDatabase.put("E001", "Ravi Kumar");
employeeDatabase.put("E002", "Priya Sharma");
employeeDatabase.put("E003", "Vijay Singh");
// Employee ID to be searched
String employeeIdToSearch = "E002";
// Checking if the employee ID exists in the database
if (employeeDatabase.containsKey(employeeIdToSearch)) {
System.out.println("Employee ID " + employeeIdToSearch + " exists. Employee Name: " + employeeDatabase.get(employeeIdToSearch));
} else {
System.out.println("Employee ID " + employeeIdToSearch + " does not exist.");
}
}
}
Output:
Employee ID E002 exists. Employee Name: Priya Sharma
Conclusion
The HashMap.containsKey() method in Java provides a way to check if a specific key is present in a HashMap. By understanding how to use this method, you can efficiently verify the presence of keys and make decisions based on their existence. This method is useful when you need to ensure that a key exists before performing operations that depend on its presence, such as updating records or processing transactions.
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