Let us say, we have a Map that should contain,
John, Is a Name Java, Is a Language
As Key and Value pair.
Let us insert above values to the Map in the below example.
import java.util.*; public class MyApplication { public static void main(String[] args) { Mapx = new HashMap<>(); x.put("John", "Is a Name"); x.put("Java", "Is a Language"); System.out.println(x); } }
So, in the above code we have created a Map,
Mapx = new HashMap<>();
And used the put() method to insert values to it.
x.put("John", "Is a Name"); x.put("Java", "Is a Language");
Below is the way how the values are placed in the Map as Key value pairs. Where John is the Key and Is a Name is the value.
So, 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 insert a new entry,
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.
import java.util.*; public class MyApplication { public static void main(String[] args) { Mapx = 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); } }
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");
But if we see the output.