Hash Tables, better known as Hash Maps, are just collections of key-value pairs. In other words, they are pieces of data (values) mapped to unique identifiers called keys.
Hash Maps were designed to give us a way of, given a key, associating a value with it for very quick lookups.
In Go, the Hash Map is implemented as a built-in map type.
Creation:
You create an empty Hash Map by declaring a map[keyType]valueType, where the keyType and valueType are the data types of the keys and values.
Let's declare a Hash Map that maps text values representing digits to their numerical value (e.g. "two" => 2). We wil map strings to integers, so the two types are string and int. We will name the map digitValue
digitValue := map[string]int{}
Creating an empty map takes O(1) time.
Initialization:
You can also add some key-value pairs in your map at creation:
digitValue := map[string]int{
"zero": 0,
"one": 1,
}
Accessing values:
A value is retrieved from a map by specifying its corresponding key in square brackets ([]):
digitValue := map[string]int{
"zero": 0,
"one": 1,
}
// Accessing values:
fmt.Println(digitValue["zero"]) // prints 0
fmt.Println(digitValue["one"]) // prints 1
fmt.Println(digitValue["three"]) // prints 0
As you can see, if you refer to a key that is not in the map, Go returns a zero value (0 for numbers, empty string for strings). Unlike some other languages, accessing a non-existing key in Go does not add it to the map.
Accessing a value from a map takes O(1) time.
Adding an entry:
Adding an entry to an existing map is simply a matter of assigning a new key and value, via the assignment operator:
digitValue := map[string]int{
"zero": 0,
"one": 1,
}
// Adding new entries:
digitValue["three"] = 3
//The new Hash Map:
// {
// "zero": 0,
// "one": 1,
// "three": 3
//}
Adding a new entry to a map takes O(1) time.
Updating an entry:
If you want to update an entry, you can just assign a new value to an existing key:
digitValue := map[string]int{
"zero": 0,
"one": 1,
}
// Updating entries:
digitValue["zero"] = 5
//The new Hash Map:
// {
// "zero": 5,
// "one": 1
//}
Updating an entry from a map takes O(1) time.
Deleting entries:
The delete() built-in function allows you to remove a key from a map:
digitValue := map[string]int{
"zero": 0,
"one": 1,
}
// Removing an entry
delete(digitValue, "zero")
//The new Hash Map:
// {
// "one": 1
//}
If you try to delete a key that is not in the map, it won't do anything.
Deleting an entry from a map takes O(1) time.
Checking if a key exists:
You can check if a key exists in a map using the comma-ok idiom:
digitValue := map[string]int{
"zero": 0,
"one": 1,
}
_, ok := digitValue["zero"]
fmt.Println(ok) // prints true since the key exists
_, ok = digitValue["three"]
fmt.Println(ok) // prints false since the key does not exist in the map
Checking if a key exists in the map is O(1) time.
Iterating over the map keys:
If we want to iterate over keys of the map, we can use the for loop along with the range keyword:
digitValue := map[string]int{
"zero": 0,
"one": 1,
}
for key := range digitValue {
fmt.Println(key, "is", digitValue[key])
}
// This will print:
// one is 1
// zero is 0
Notice the order is not the same as initiated. map keeps the data in random order
Iterating over a map takes O(n) time.
Iterating using both the key and the value for simplicity:
If we want to iterate over both keys and values of the map, we can use the for loop along with the range keyword as follows:
digitValue := map[string]int{
"zero": 0,
"one": 1,
}
for key, val := range digitValue {
fmt.Println(key, "is", val)
}
// This will print:
// one is 1
// zero is 0
Notice the order is not the same as initiated. map keeps the data in random order
Iterating over a map takes O(n) time.
Assignment
Follow the Coding Tutorial and let's practice with maps.
Hint
Look at the examples above if you get stuck.
Free plan: 30 tutor messages. PRO is unlimited.
