在本教程中,我们将学习如何使用Golang编程语言将数组转换为集合。
集合是一个项目的集合。你可以对这些项目进行迭代/添加新的/删除项目,清除集合,获得大小并检查集合是否包含任何值。集合中的一个对象可能只被存储一次,不能重复。
语法
var myset map[type]struct{}
Key是你想创建的一种数据类型。Struct{}是一个空结构,大小为0字节。
例1:使用For Loop将数组转换为集合的Go代码
算法
- 第1步 – 导入软件包 fmt。
-
第2步 – 启动函数main()。
-
第3步 – 创建一个集合’a1’并声明它。
-
第4步 – 将数组的元素添加到集合a1中。
-
第5步 – 使用带有range fore的for循环来遍历元素。
-
第6步 – 检查元素是否存在于这个集合中。
-
第7步 – 使用delete()函数来删除元素。
-
第8步 – 使用fmt.Println()在屏幕上打印出最终结果。
例子
package main
// fmt package allows us to print anything on the screen
import "fmt"
// start the function main()
// this function is the entry point of the executable program
func main() {
// Create and Declaration of a set
// a1 acts as a set of strings
a1 := make(map[string]struct{})
fmt.Println(a1) // map[]
//Adding elements to Set
// by setting the value of each key to an
// empty struct
var empty = struct{}{}
a1["five"] = empty
a1["four"] = empty
a1["six"] = empty
fmt.Println(a1) // map[five:{} four:{} six:{}]
fmt.Println(len(a1)) // 3
//Iteration of elements of a Set using for loop with a range form
for v := range a1 {
fmt.Println(v)
}
// Check if elements exist in a set
if _, ok := a1["five"]; ok {
fmt.Println("exists in a set ") // Element exists
}
if _, ok := a1["one"]; ok {
fmt.Println("Not exists in a set.") // Element not exists
}
// use the delete() function of the map to remove elements from a set
delete(a1, "five")
fmt.Println(len(a1)) // 2
// print the final results
}
输出
map[]
map[five:{} four:{} six:{}]
3
five
four
six
exists in a set
2
说明
-
在上面的程序中,我们首先声明包main。main包是用来告诉Go语言编译器,该包必须被编译并产生可执行文件。
-
我们导入了fmt包,该包包括fmt包的文件,然后我们可以使用与fmt包相关的函数
-
现在启动函数main(),这个函数是可执行程序的入口。它不接受任何参数,也不返回任何东西。
-
现在我们创建一个接受字符串值的空集合’a1’,并声明它。这里s1:= make(map[string]struct{})是一个空集合,有一个字符串键和空结构–struct{}。
-
接下来,我们使用向map添加元素的语法将元素添加到该集合中,以声明一个空结构。在代码的第23、24和25行,代码向集合添加了三个元素 一旦数据被添加到集合中
-
接下来,我们使用带有范围形式的for循环来迭代集合中的元素。
-
接下来,我们将使用两个值表达式检查集合中的元素,使用从地图中获取项目。在代码的第35行: if _, ok:= a1[“five”]; ok { :: 这里返回两个值 第一个值是一个空结构,不是必须的,所以用空白标识符(_)代替 第二个参数是一个布尔值 – 如果存在,返回ok=true;如果不存在,返回ok=false。
-
接下来,我们使用map的delete()函数来移除集合中的元素。我们可以使用Go内置的delete()函数从地图中删除一个项目。
-
最后我们使用fmt.Println()函数打印结果
-
我们能够执行所有具有集合特征的操作,从集合中添加和删除成员的时间复杂度同样为O(1)。
例2:将数组转换为集合的更简单的go代码
算法
-
第1步 – 导入软件包 fmt。
-
第2步 – 启动函数main()。
-
第3步 – 创建一个集合’fruit’并声明它。
-
第4步 – 将数组中的元素添加到集合fruit中。
-
第5步 – 使用fmt.Println()在屏幕上打印最终结果。
例子
// GOLANG PROGRAM TO CONVERT ARRAY TO SET
// We can implement set using Map types.
// Declare the package main
package main
// fmt package allows us to print anything on the screen
import "fmt"
// start the function main ()
// this function is the entry point of the executable program
func main() {
// Create and Declaration of a set
// fruit acts as a set of strings
fruit := map[string]struct{}{}
fmt.Println("fruit")
// We can add members to the set
// by setting the value of each key to an
// empty struct
fruit["apple"] = struct{}{}
fruit["banana"] = struct{}{}
fruit["Grapes"] = struct{}{}
fruit["Orange"] = struct{}{}
// Adding a new member
fruit["Kiwi"] = struct{}{}
// Adding an existing to the set
fruit["Grapes"] = struct{}{}
// Removing a member
delete(fruit, "banana")
fmt.Println(fruit)
}
输出
fruit
map[Grapes:{} Kiwi:{} Orange:{} apple:{}]
说明
在上面的代码中,我们使用数组元素创建了一个集合,并展示了一个没有使用任何条件语句的简单代码。我们能够执行集合的所有操作,对于从集合中添加和删除成员,其时间复杂度同样为O(1)。