Map2
map增加和更新:
map["key"] = value //如果key还没有,就是增加,如果key存在就是修改
案例演示:
func main() {
cities := make(map[string]string)
cities["no1"] = "北京"
cities["no2"] = "天津"
cities["no3"] = "上海"
//因为 no3这个key已经存在,因此下面的这句话就是修改
cities["no3"] = "上海~"
fmt.Println(cities)
}
map删除:
说明:
delete(map, "key"), delete 是一个内置函数,如果key存在,就删除该key-value,如果key不存在,不操作,但是也不会报错。
func delete
func delete(m map[Type]Type1, key Type)
delete 内建函数按照指定的键将元素从映射中删除。 若 m 为 nil 或无此元素,delete 即为空操作。
案例演示:
func main() {
cities := make(map[string]string)
cities["no1"] = "北京"
cities["no2"] = "天津"
cities["no3"] = "上海"
//因为 no3这个key已经存在,因此下面的这句话就是修改
cities["no3"] = "上海~"
fmt.Println(cities)
delete(cities, "no1")
fmt.Println(cities)
//当delete指定的key不存在时,删除不会操作,也不会报错
delete(cities, "no4")
fmt.Println(cities)
}
细节说明:
1)如果我们要删除map的所有key,没有一个专门的方法一次删除,可以遍历一下key,逐个删除
2)或者 map = make(...),make一个新的,让原来的称为垃圾,被gc回收。
func main() {
cities := make(map[string]string)
cities["no1"] = "北京"
cities["no2"] = "天津"
cities["no3"] = "上海"
cities = make(map[string]string)
fmt.Println(cities)
}
map查找:
案例演示:
func main() {
cities := make(map[string]string)
cities["no1"] = "北京"
cities["no2"] = "天津"
cities["no3"] = "上海"
val, ok := cities["no2"]
if ok {
fmt.Printf("有no2 key 值为%v", val)
} else {
fmt.Printf("没有no2 key\n")
}
}
对上面的代码的说明:
如果 cities 这个map中存在"no2",那么ok 就会返回true,否则返回false。
map遍历:
案例演示相对复杂的map遍历:该map的value 又是一个map
说明:map的遍历使用for-range的结构遍历
案例演示:
func main() {
//使用for-range遍历map
cities := make(map[string]string)
cities["no1"] = "北京"
cities["no2"] = "天津"
cities["no3"] = "上海"
for k, v := range cities {
fmt.Printf("k=%v v=%v \n", k, v)
}
//使用for-range遍历一个结构比较复杂的map
studentMap := make(map[string]map[string]string, 10)
studentMap["stu01"] = make(map[string]string, 3)
studentMap["stu01"]["name"] = "tom"
studentMap["stu01"]["sex"] = "男"
studentMap["stu01"]["address"] = "北京长安街"
studentMap["stu02"] = make(map[string]string, 3) //这句话不能少
studentMap["stu02"]["name"] = "mary"
studentMap["stu02"]["sex"] = "女"
studentMap["stu02"]["address"] = "北京东沙村"
for k1, v1 := range studentMap {
fmt.Println("k1=",k1)
for k2, v2 := range v1 {
fmt.Printf("\t k2=%v v2=%v \n", k2, v2)
}
fmt.Println()
}
}
map的长度:
func len
func len(v Type) int
len 内建函数返回 v 的长度,这取决于具体类型:
数组:v 中元素的数量。
数组指针:*v 中元素的数量(即使 v 为 nil)。
切片或映射:v 中元素的数量;若 v 为 nil,len(v) 即为零。
字符串:v 中字节的数量。
信道:信道缓存中队列(未读取)元素的数量;若 v 为 nil,len(v) 即为零。
案例演示:fmt.Println(len(stus))
func main() {
//使用for-range遍历map
cities := make(map[string]string)
cities["no1"] = "北京"
cities["no2"] = "天津"
cities["no3"] = "上海"
fmt.Println("cities 有", len(cities), "对 key-value")
//使用for-range遍历一个结构比较复杂的map
studentMap := make(map[string]map[string]string, 10)
studentMap["stu01"] = make(map[string]string, 3)
studentMap["stu01"]["name"] = "tom"
studentMap["stu01"]["sex"] = "男"
studentMap["stu01"]["address"] = "北京长安街"
studentMap["stu02"] = make(map[string]string, 3) //这句话不能少
studentMap["stu02"]["name"] = "mary"
studentMap["stu02"]["sex"] = "女"
studentMap["stu02"]["address"] = "北京东沙村"
fmt.Println("studentMap 有", len(studentMap), "对 key-value")
}
Map2的更多相关文章
- Sass Maps的函数-map-values($map)、map-merge($map1,$map2)
map-values($map) map-values($map) 函数类似于 map-keys($map) 功能,不同的是 map-values($map )获取的是 $map 的所有 value ...
- java 集合(Map2)
Map 接口的迭代方法: import java.util.*; public class ex12 { public static void main(String[] args) { Map< ...
- Scala中List(Map1,Map2,Map3 ....) 转成一个Map
这个问题研究好久...头大,不记得有fold用法了. fold函数:折叠,提供一个输入参数作为初始值,然后大括号中应用自定义fun函数并返回值. list.fold(Map()){(x,y)=> ...
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- Java基础Map接口+Collections
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- [Hadoop in Action] 第5章 高阶MapReduce
链接多个MapReduce作业 执行多个数据集的联结 生成Bloom filter 1.链接MapReduce作业 [顺序链接MapReduce作业] mapreduce-1 | mapr ...
- 龙之谷手游WebVR技术分享
主要面向Web前端工程师,需要一定Javascript及three.js基础:本文主要分享内容为基于three.js开发WebVR思路及碰到的问题:有兴趣的同学,欢迎跟帖讨论. 目录:一.项目体验1. ...
- Map集合
1:Map (1)将键映射到值的对象. 一个映射不能包含重复的键:每个键最多只能映射到一个值. 键值对的方式存在 (2)Map和Collection的区别? A:Map 存储的是键值对形式的元素,键唯 ...
- MapReduce工作流多种实现方式
学习 hadoop,必不可少的就是编写 MapReduce 程序.当然,对于简单的分析程序,我们只需一个 MapReduce 任务就能搞定,然而对于比较复杂的分析程序,我们可能需要多个Job或者多个M ...
随机推荐
- 一个完整的HTTP请求过程详细
整个流程1.域名解析 —> 2.与服务器建立连接 —> 3.发起HTTP请求 —>4. 服务器响应HTTP请求,浏览器得到html代码 —> 5.浏览器解析html代码,并请求 ...
- SQLZOO 习题
https://sqlzoo.net 8. 美國.印度和中國(USA, India, China)是人口又大,同時面積又大的國家.排除這些國家. 顯示以人口或面積為大國的國家,但不能同時兩者.顯示國家 ...
- Linux--shell grep与正则表达式--04
一.grep程序 Linux下有文本处理三剑客:grep.sed.awk grep:文本 行过滤工具 sed:文本 行编辑器(流编辑器) awk:报告生成器(做文本输出格式化) 1.grep grep ...
- linux ab压力测试
1.安装 yum -y install httpd-tools 2.检测版本 ab -V 3.常用 ab -c -n 127.0.0.1/index.php #同时处理100个请求并运行10次inde ...
- Debug to add expression
Debug expression
- 10 | MySQL为什么有时候会选错索引? 学习记录
<MySQL实战45讲>10 | MySQL为什么有时候会选错索引? 学习记录http://naotu.baidu.com/file/e7c521276650e80fe24584bc9a6 ...
- 【LeetCode】位运算 bit manipulation(共32题)
[78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...
- 载]mysqlhotcopy 热备工具体验与总结
载]mysqlhotcopy 热备工具体验与总结 今天有空尝试了一下MYSQLHOTCOPY这个快速热备MYISAM引擎的工具.(本文是针对单个服务器的情况,以后将会加入多服务器相关操作)他和MYSQ ...
- Linux g++ 编译添加 pthread
If you are going to compile a C program with pthread.h in LINUX using GCC or G++ you will have to us ...
- APP稳定性测试-monkey执行
Monkey命令行可用的全部选项 *示例 : adb shell monkey -p cn.lejiayuan.alpha --pct-touch 30 --pct-motion 15 --pct-t ...