
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Inverted Hash Collection in Go
In golang, we can print the inverted hash collection using the inverted mapping method. Hashmap stored the data in pair of key:Value in hash collection and that reduces the execution time. In this article we are going to see two different example to understand how to create a golang program that will print an inverted hash collection.
Syntax
func make ([] type, size, capacity)
The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments.
func len(v Type) int
The len() function is used to get the length of a any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable.
Algorithm
Import the desired packages in the program
Create a main function
In that particular function create a hashmap
Then, create an inverted map using internal functions
Print the inverted map on the console
Example 1
In this example, we will create a hashmap and an additional map named inverted to get the inverted output i.e. key is assigned to value. The hashmap will be iterated to get the inverted output in every iteration. Let's understand this example and have a close look at the code and the algorithm.
//Golang program to print the inverted hash collection package main import "fmt" //Main to execute the program func main() { // Create a map with keys of type string and values of type int hashmap := map[string]int{"apple": 100, "mango": 180, "banana": 120} // Create a new map with inverted keys and values inverted := make(map[int]string) for k, element := range hashmap { inverted[element] = k } // Print the inverted map fmt.Println("This is the following inverted map:") fmt.Println(inverted) }
Output
This is the following inverted map: map[100:apple 120:banana 180:mango]
Example 2
In this illustration, we will use len(map) in the make function to create inverted map which will be used to store the inverted key:value pairs. The output will be an inverted map printed on the console using fmt package. Let's see the execution via code and the algorithm.
//Golang program to print the inverted hash collection package main import "fmt" //Main function to execute the program func main() { // Create a map with keys of type string and values of type int hashmap := map[string]int{"apple": 100, "mango": 120, "banana": 130} // Create a new map with inverted keys and values inverted := make(map[int]string, len(hashmap)) for k, element := range hashmap { inverted[element] = k } // Print the inverted map fmt.Println("The inverted map is presented as follows:") fmt.Println(inverted) }
Output
The inverted map is presented as follows: map[100:apple 120:mango 130:banana]
Conclusion
We executed the program of printing the inverted hash collection using two examples. In the first example we created the additional map without using len(hashmap) whereas in the second example we created the map using len(hashmap) function.