Golang程序将项目存储到哈希集合中
在 Golang 中,哈希映射是哈希集合的一部分,它以键值对的形式存储值。在这篇文章中,我们将使用两个不同的示例将项目存储到哈希集合中。在第一个示例中,将使用索引将项目存储到映射中,在第二个示例中,将使用项目结构体来存储项目。
语法
func make ([] type, size, capacity)
Go 语言中的 make 函数用于创建数组/映射,它接受要创建的变量的类型、大小和容量作为参数。
算法
创建一个 package main 并声明程序中的 fmt(格式化包),其中 main 生成可执行代码,而 fmt 帮助格式化输入和输出。
使用映射字面量和 make 函数创建哈希映射,其键的类型为字符串,值类型为 int。
使用索引将值添加到哈希映射中,例如 pencil=10、pen=20 和 scale=15。
添加值后,在控制台上打印哈希映射。
在此步骤中,我们将通过使用索引访问键的值来操作哈希映射。
然后,我们将再次使用索引更新键的值。
然后,我们将使用 delete 函数从映射中删除特定的键,并在控制台上打印更新后的映射。
打印语句使用 fmt 包中的 Println() 函数执行,其中 ln 表示换行。
示例 1
在此示例中,我们将使用 make 内置函数创建一个哈希映射,然后使用索引将所需的值添加到映射中。在这里,我们将使用三个值,然后使用 fmt 包在终端上打印它们,并使用不同的操作来操作哈希映射。让我们看看代码和算法以了解其实际工作原理。
package main
import "fmt"
func main() {
// Initialize an empty map with string keys and integer values
hashmap := make(map[string]int)
// Add some items to the map
hashmap["pencil"] = 10
hashmap["pen"] = 20
hashmap["scale"] = 15
// Print the entire map
fmt.Println("The hashmap created above is: ")
fmt.Println(hashmap)
// Access a specific item by its key
fmt.Println("The value of specific element from hashmap is:")
fmt.Println(hashmap["pencil"])
// Update the value of an existing item
hashmap["scale"] = 20
// Delete an item from the map
delete(hashmap, "pencil")
// Print the updated map
fmt.Println("The hashmap after deleting an item from it is:")
fmt.Println(hashmap)
}
输出
The hashmap created above is: map[pen:20 pencil:10 scale:15] The value of specific element from hashmap is: 10 The hashmap after deleting an item from it is: map[pen:20 scale:20]
示例 2
在此示例中,我们将像在最后一个示例中一样创建一个哈希映射,但这里我们还将创建一个项目结构体实例来创建键值对,然后将这些项目添加到哈希映射中并操作哈希映射。输出将使用 fmt 包打印。让我们看看代码和算法。
package main
import "fmt"
// Define a struct to represent an item
type Item struct {
Name string
Quantity int
}
func main() {
// Initialize an empty map with string keys and Item values
hashmap := make(map[string]Item)
// Create item instances and add them to hashmap
pen := Item{Name: "pen", Quantity: 10}
pencil := Item{Name: "pencil", Quantity: 20}
registers := Item{Name: "registers", Quantity: 30}
hashmap[pen.Name] = pen
hashmap[pencil.Name] = pencil
hashmap[registers.Name] = registers
// Print the entire map
fmt.Println("The entire map is presented as follows: ")
fmt.Println(hashmap)
// Access a specific item by its key
fmt.Println("The value of the element is accessed as follows:")
fmt.Println(hashmap["pencil"])
// Update the value of an existing item
pen.Quantity = 50
hashmap[pen.Name] = pen
// Delete an item from the map
delete(hashmap, pencil.Name)
// Print the updated map
fmt.Println("The updated map after deleting the data item is:")
fmt.Println(hashmap)
}
输出
The entire map is presented as follows:
map[pen:{pen 10} pencil:{pencil 20} registers:{registers 30}]
The value of the element is accessed as follows:
{pencil 20}
The updated map after deleting the data item is:
map[pen:{pen 50} registers:{registers 30}]
结论
我们使用两个示例执行了将项目存储到哈希集合中的程序。在第一个示例中,我们使用索引来存储项目,在第二个示例中,我们使用结构体来存储项目。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP