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 ...
随机推荐
- C#将MD5后的字符串转为字符数据,随机大小写
一如下代码 public static string GenerateCode(Guid id, DateTime endTime, string Type) { string str = id + ...
- spark复习笔记(2)
之前工作的时候经常用,隔了段时间,现在学校要用学的东西也忘了,翻翻书谢谢博客吧. 1.什么是spark? Spark是一种快速.通用.可扩展的大数据分析引擎,2009年诞生于加州大学伯克利分校AMPL ...
- python OpenCV视频的读取及保存
import cv2 cap = cv2.VideoCapture('rtsp://admin:hik12345@192.168.3.160/Streaming/Channels/1') fourcc ...
- 零点.Net Core 接触
一.Program.cs类与Startup类 1.一切从Main开始,Main方法包含了是整个应用程序的入口 ASP.NET Core应用程序可以配置和启动主机(Host). 主机负责应用程序启动和生 ...
- 小程序Page里的函数比app.js先执行的解决办法
问题描述: 当我们初始化一个小程序时,默认文件 app.js 中有onLaunch函数, onLaunch: function () { console.log("onLaunch" ...
- python笔记(2)---不定长参数
python自定义函数中有两种不定长参数, 第一种是*name:加了星号 * 的参数会以元组(tuple)的形式导入 第二种是**name:加了星号 * *的参数会以字典(dict)的形式导入 *na ...
- 【串线篇】spring boot日志使用
一.默认配置 1.SpringBoot默认帮我们配置好了日志: //记录器 Logger logger = LoggerFactory.getLogger(getClass()); @Test pub ...
- Python---编辑器安装和print函数
Python---编辑器安装和print函数 -------------------------------------------------------- 一.Python是什么? Python是 ...
- tar解压命令
解压 tar –xvf file.tar //解压 tar包 tar -xzvf file.tar.gz //解压tar.gz tar -xjvf file.tar.bz2 //解压 tar.bz2 ...
- [HG]奋斗赛G
T1 题目描述 安娜斯塔西娅喜欢去乌日扬迪安中央公园散步. 但她对简单的散步不感兴趣,于是她开始收集公园里的鹅卵石. 一开始,她决定收集所有她能在公园里找到的鹅卵石. 她只有两个口袋. 她能在每个口袋 ...