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




GO - MAP


A 'Map' is a Collection that can also hold multiple values in 'Key' and 'Value' pairs.


You can think of a 'Map' like an actual English Dictionaries. Where you search for a word and you get its explanation.


The declaration of a 'Map' is also quite simple. You can place the multiple values inside braces '{}' in the form of 'Key' and 'Value' pairs.


Just imagine the 'Key' to be the word you are going to search in the English Dictionary.And the 'Value' is the explanation you find in it.


Creating a 'Map'


Example :



package main
import "fmt"
    
func main() {
    
    x := map[string]string {
        "Pune" : "Is a City", 
        "John": "Is a Name", 
        "Go": "Is a Language"}
                
    fmt.Println(x)			 	
}


Output :



 map[Go:Is a Language John:Is a Name Pune:Is a City]

So, in the above code we have created a 'Map' using square braces '{}'. And the values are represented in 'Key' and 'Value' pairs.


A 'Map' should begin with the 'map' keyword and there are two data types. One for the key and other for the value.


java_Collections

The 'Key' is a String and in the value we have provided its explanation(Somewhat like the English Map).


i.e. We know that the first element 'Pune' is a String. So, in the 'Key', we have put the String 'Pine' and in its value, we have put the explanation, 'Is a City'. Same logic is applicable for the other two 'Keys', 'John' and 'Go'.


A 'Key' and 'Value' is separated by ':'.


x := map[string]string {
    "Pune" : "Is a City", 
    "John": "Is a Name", 
    "Go": "Is a Language"}

And initialised to the variable 'x'.


java_Collections

And in the next line we have printed the 'Map' using the print statement.


fmt.Println(x)

Now, if we see the output,


Output :



 map[Go:Is a Language John:Is a Name Pune:Is a City]

Iterating a Map using 'for range loop' and getting the 'Keys'


Example :



package main
import "fmt"
    
func main() {
    
    x := map[string]string {
        "Pune" : "Is a City", 
        "John": "Is a Name", 
        "Go": "Is a Language"}
            
    for i := range x {
        fmt.Println("The key is :",i)
    }				 	
}


Output :



 The key is : Pune
 The key is : John
 The key is : Go


Similarly, in the above code we have created a 'Map' using braces '{}' and 'Key' and 'Value' pairs.


x := map[string]string {
	"Pune" : "Is a City", 
	"John": "Is a Name", 
	"Go": "Is a Language"}

And initialised to the variable 'x'.


java_Collections

In the next line we have used the 'for range loop' to Iterate through the 'Map'.


for i := range x {
    fmt.Println("The key is :",i)
}    

As we know, there is a 'Key' and 'Value' pair in a 'Map'. But we are just getting the 'Keys'


Let us understand it in the iterations of 'for' loop,


for i := range x {   

1st Iteration


In the first Iteration the first 'Key' of the 'Map' 'x' (i.e. The key 'Pune') is taken and put into the variables 'i'.


java_Collections

And the print statement, prints the value of 'i'(i.e. The key).



 Pune

2nd Iteration


Similarly, in the second Iteration the second 'Key' of the 'Map' 'x' (i.e. The key 'John') is taken and put into the variables 'i'.


java_Collections

And the print statement, prints the value of 'i'(i.e. The key).



 Pune
 John

3rd Iteration


Similarly, in the third Iteration the third 'Key' of the 'Map' 'x' (i.e. The key 'Go') is taken and put into the variables 'i'.


java_Collections

And the print statement, prints the value of 'i'(i.e. The key).



 Pune
 John
 Go

So, we have seen how to get the 'Keys'. Now, let us see how can we get the corresponding 'Value' associated with that 'Key'.


It is just like searching a word in an English Map and get its corresponding value.


Iterating a Map using 'for loop' and getting the 'Value' for a 'Key'


Example :



    package main
    import "fmt"
    
    func main() {
    
        x := map[string]string {
            "Pune" : "Is a City", 
            "John": "Is a Name", 
            "Go": "Is a Language"}
        
        for i := range x {
            fmt.Println("The value for the key ",i," is ",x[i])
        }				 	
    }


Output :



 The value for the key Pune is Is a City
 The value for the key John is Is a Name
 The value for the key Go is Is a Language

Similarly, in the above code we have created a 'Map' using braces '{}' and 'Key' and 'Value' pairs.


x := map[string]string {
    "Pune" : "Is a City", 
    "John": "Is a Name", 
    "Go": "Is a Language"} 

And initialised to the variable 'x'.


java_Collections

In the next line we have used the 'for range loop' to Iterate through the 'Map'.


for i := range x {
	fmt.Println("The value for the key ",i," is ",x[i])
}

As we know, there is a 'Key' and 'Value' pair in a 'Map'. And in each Iteration, we are fetching the 'Value' for a particular 'Key'.


Let us understand it in the iterations of 'for' loop,


for i := range x {

1st Iteration


In the first Iteration the first 'Key' of the 'Map' 'x' (i.e. The key 'Pune') is taken and put into the variables 'i'.


java_Collections

Then in the print statement, we have used the 'Key' (i.e. 'Pune') to get the value associated to it 'x[i]')


java_Collections

Where,


java_Collections

And the print statement,


fmt.Println("The value for the key ",i," is ",x[i])

Prints the 'Key' and 'Value'.



 The value for the key Pune is Is a City

2nd Iteration


Similarly, in the second Iteration the second 'Key' of the 'Map' 'x' (i.e. The key 'John') is taken and put into the variables 'i'.


java_Collections

Then in the print statement, we have used the 'Key' (i.e. 'John') to get the value associated to it 'x[i]')


java_Collections

And the print statement,


fmt.Println("The value for the key ",i," is ",x[i])

Prints the 'Key' and 'Value'.



 The value for the key Pune is Is a City
 The value for the key John is Is a Name

3rd Iteration


Similarly, in the third Iteration the third 'Key' of the 'Map' 'x' (i.e. The key 'John') is taken and put into the variables 'i'.


java_Collections

Then in the print statement, we have used the 'Key' (i.e. 'Go') to get the value associated to it 'x[i]')


java_Collections

And the print statement,


fmt.Println("The value for the key ",i," is ",x[i])

Prints the 'Key' and 'Value'.



 The value for the key Pune is Is a City
 The value for the key John is Is a Name
 The value for the key Go is Is a Language

Iterating a Map using 'for range loop' to get both the Key and Value


Example :



package main
import "fmt"

func main() {

	x := map[string]string {
		"Pune" : "Is a City", 
		"John": "Is a Name", 
		"Go": "Is a Language"}
	
	for i,j := range x {
		fmt.Println("The value for the key ",i," is ",j)
	}				 	
}


Output :



 The value for the key Pune is Is a City
 The value for the key John is Is a Name
 The value for the key Go is Is a Language

Similarly, in the above code we have created a 'Map' using braces '{}' and 'Key' and 'Value' pairs.


x := map[string]string {
	"Pune" : "Is a City", 
	"John": "Is a Name", 
	"Go": "Is a Language"} 

And initialised to the variable 'x'.


java_Collections

In the next line we have used the 'for loop' to Iterate through the 'Map'.


for i,j := range x {
	fmt.Println("The value for the key ",i," is ",j)
}

Now, since there is a 'Key' and 'Value' pair in a 'Map', two variables 'i' and 'j' are used. 'i' to hold the key and 'j' to hold the value.


And to get all the elements of the 'Map', the 'items()' Function is used.


Now, if we see the iterations of 'for' loop,


for i,j := range x {

1st Iteration


In the first Iteration the first value of the 'Map' 'x' (i.e. The key '5' and the value 'Is a Number') is taken and put into the variables 'i' and 'j'.


java_Collections

And the print statement, prints the value of 'i'(i.e. The key) and 'j'(i.e. The value).



 The value for the key Pune is Is a City

2nd Iteration


Similarly, in the second Iteration the second value of the 'Map' 'x' (i.e. The key 'John' and the value 'Is a Name') is taken and put into the variables 'i' and 'j'.


java_Collections

And the print statement, prints the value of 'i'(i.e. The key) and 'j'(i.e. The value).



 The value for the key Pune is Is a City
 The value for the key John is Is a Name

3rd Iteration


Similarly, in the third Iteration the third value of the 'Map' 'x' (i.e. The key 'Go' and the value 'Is a Language') is taken and put into the variables 'i' and 'j'.


java_Collections

And the print statement, prints the value of 'i'(i.e. The key) and 'j'(i.e. The value).



 The value for the key Pune is Is a City
 The value for the key John is Is a Name
 The value for the key Go is Is a Language

So far, we have seen, how to access all the elements of a 'Map'. i.e. Both the 'Key' and 'Value'.


Now, what if we want to access just the 'Key' or the 'Value'. Let us see in the next example.


Next, let us see, how to access the elements of the Map.