Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Java - Update Map


How to update the entries of a Map?


we have a Map that contains,


John, Is a Name
Java, Is a Language

As Key and Value pair.


Now, let us say, we want to update the entry,


John, Is a Name

With


John, Is an English Name

Where John is the Key and Is an English Name is its corresponding Value.


Let us see in the below example.


Example :



import java.util.*;

public class MyApplication {
    public static void main(String[] args) {

        Map x = new HashMap<>();

        x.put("John", "Is a Name");
        x.put("Java", "Is a Language");

        x.put("John", "Is an English Name");

        System.out.println(x);
    }
}


Output :



  {Java=Is a Language, John=Is an English Name}

In the above example, we have the Map with the following Entries.

Spring_Boot

So, for the Key John, the initial value was Is a Name.


x.put("John", "Is a Name");

And we wanted to add a new entry with the Key as John and the Value as Is an English Name.


x.put("John", "Is an English Name");

Now, if we see the output.

Output :



  {Java=Is a Language, John=Is an English Name}

We can see that the Key John is updated with the new value Is an English Name.

Spring_Boot