Basic knowledge of Map theory
- Similar to a hash table or dictionary in other languages, storing data in key-value form
- Key must be a type that supports the = = or! = comparison operation, cannot be a function, map, or slice
- Map lookup is much faster than linear search, but 100 times times slower than using indexed access data
Map using make () to create, support: = This shorthand method
- Make ([Keytype]valuetype, CAP), cap represents capacity, can be omitted
- Capacity is automatically expanded when exceeded, but as far as possible to provide a reasonable initial value
Use Len () to get the number of elements
- A key-value pair is added automatically when it does not exist, and a key-value pair is deleted using Delete ()
- Use for range to iterate over map and slice
Considerations when iterating
Map Exercise Package Mainimport ("FMT" "sort") Func main () {//============================ mode one: ======================= = = = =//Create an empty map//Declare first a map type var nodemap map[int]string//Initialize Nodemap, all empty nodemap = map[int]string{} f Mt. Println (Nodemap) fmt. Println ("-----------------------------------------------")//============================ Mode II: =========== use make= =============== var Clustermap map[int]string = Make (map[int]string) fmt. Println (Clustermap) fmt. Println ("-----------------------------------------------")//============================ mode three: =========== use inference function = =============== Operatormap: = Make (map[int]string) fmt. Println (OPERATORMAP)//============================ Mode IV: =========== directly initialized when created ================//This way, do not need to use make opera TORMAP2: = map[int]string{3: "Hello", 5: "World"} FMT. Println ("===>:\t", OPERATORMAP2)//====== initialize ===== nodemap[1] = "Sparknode" nodemap[2] = "Esnode" FMT. Println (nodemap)//======= remove element by key NodeName: = nodemap[2] FMT. Println (nodeName)//====== Delete the key value pair Delete (Nodemap, 1)//delete FMT According to the key value pair. Println (Nodemap) fmt. Println ("-----------------------------------------------")//--------------operation of Complex map-------------------// Declare a map type var clusterapptaskid map[string]map[string]string//Initialize this map type Clusterapptaskid = Make (map[string]map[st ring]string) taskId, OK: = clusterapptaskid["Spark-beijing" ["/SPARK-BEIJING/APP-UEWQR"] if!ok {//each level of map There is initialization, is not found at compile time, only run time, can discover clusterapptaskid["spark-beijing"] = Make (map[string]string)} clusterapptaskid["sp Ark-beijing "["/spark-beijing/app-uewqr "] =" app-ewr-spark-taskid-001 "taskid, OK = clusterapptaskid[" spark-beijing " ["/SPARK-BEIJING/APP-UEWQR"] fmt. Println (TaskId, OK) fmt. Println ("-----------------------------------------------")//--------------iterative Operation-------------------//for i, V: = Ran GE Slice {////}//i, denotes subscript, v denotes the corresponding value, is the value of the copy//to pay special attention to any modification of V, does not affect the original value,//MAP type is also, does not affect the original value of///For example, the following example, is the operation of V, will not affect the SM//Therefore, it is not recommended to use this way SM: = Make ([]map[int]string, 5) for _, V: = Range SM {v = make (map[int]string) v[1] = "OK" FMT. Println (v)} FMT. PRINTLN (SM) fmt. Println ("-----------------------------------------------")//modify below, original value for I: = range sm {Sm[i] = make (map[in t]string) sm[i][2] = "Spark" FMT. Println (Sm[i])} FMT. PRINTLN (SM) fmt. Println ("-----------------------------------------------")//---------------------------------//map is unordered, How to get the values in map from small to large by key//You need to generate a slice to store the map's key//key in order from small to large, then, according to the key to the value Marathonapp: = map[int]string{1: "Spark", 3: "Es", 8: "FTP", 7: "Hadoop", 4: "K8s", 2: "Docker"} len: = Len (Marathonapp)//Generate a slice to store the key kslice: = Make ([]int, Len) Declares a counter for initializing slices using var i int = 0 for k, _: = Range Marathonapp {kslice[i] = k i++} fmt. Println ("key before ordering: \ t", kslice)//Slice is a reference pass, so after sorting below, you do not need to return a value to receive sort. Ints (Kslice) Fmt. PRINTLN ("key sorted after: \ t", Kslice) fmt. Println ("according to the key from small to large, take out the corresponding value")//below, starting to iterate Marathonapp, you can follow the key from small to large, and then remove the value for _, V: = range Kslice {//Here must note, is used Value, rather than kslice the subscript fmt. Println (Marathonapp[v])}} FMT. Println ("-----------------------------------------------")//------------------How to swap the key and value of the map type--------------- Kafkaclusteridnamemap: = map[int]string{1: "Kafka-beijing", 2: "KAFKA-SJZ", 4: "Kafka-shanghai", 5: "Kafka-tianjin", 3: " Kafka-yizhuang "} Kafkanameclusteridmap: = Make (Map[string]int) for k, V: = range Kafkaclusteridnamemap {KAFK ANAMECLUSTERIDMAP[V] = k} fmt. Println ("Kafkaclusteridnamemap:\t", Kafkaclusteridnamemap) fmt. Println ("Kafkanameclusteridmap:\t", Kafkanameclusteridmap)
Map exercises for Go language