🎓 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
1. Introduction
The HashMap class in Java is a part of the Collections Framework, which implements the Map interface. It stores data in key-value pairs, making data retrieval efficient and straightforward by using keys. The put(K key, V value) method is a crucial method in the HashMap class. It is used to add a new key-value pair to the map or update the value of an existing key. If the map previously contained a mapping for the key, the old value is replaced by the specified value. This method is fundamental for manipulating the contents of a HashMap.
2. Program Steps
1. Create a HashMap.
2. Use the put method to add new key-value pairs to the map.
3. Use the put method again to update the value associated with an existing key.
4. Display the contents of the HashMap.
3. Code Program
import java.util.HashMap;
public class HashMapPutMethodExample {
public static void main(String[] args) {
// Step 1: Creating a HashMap
HashMap<Integer, String> map = new HashMap<>();
// Step 2: Adding key-value pairs to the HashMap
map.put(1, "Ramesh");
map.put(2, "Suresh");
map.put(3, "Mahesh");
// Displaying the initial contents of the HashMap
System.out.println("Initial map: " + map);
// Step 3: Updating the value for key 2
map.put(2, "Dinesh");
// Displaying the updated contents of the HashMap
System.out.println("Updated map: " + map);
}
}
Output:
Initial map: {1=Ramesh, 2=Suresh, 3=Mahesh}
Updated map: {1=Ramesh, 2=Dinesh, 3=Mahesh}
Explanation:
1. A new HashMap is created, intended to map Integer keys to String values.
2. The put method is used to insert three key-value pairs into the map. This step demonstrates adding new data to the map.
3. The value for an existing key (2) is updated by calling the put method again with the same key but a different value ("Dinesh" replaces "Suresh"). This illustrates how put can be used to update the value associated with a specific key.
4. The outputs before and after the update show the initial contents of the map and how they change after using put to update a value. The map automatically replaces the old value with the new one for the specified key, demonstrating the key-value pair's add and update functionality within a HashMap.
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