Top

map

In different languages, "map" has many different names. The "map" in Z1h corresponds to the following languages. The names of the same concepts are similar:

language name characteristics or main differences
Z1h map Unordered, thread unsafe
Z1h Map Unordered, thread safe
Go map [string] interface {} Assignment is not allowed through s.key = value, Only s ["key"] is allowed to operate
Python set (set) Assignment is not allowed through s.key = value, only s ["key"] is allowed to operate
JavaScript Object No s ?. key for optional chain operation
Java HashMap Can only be operated by method
Objective-C NSMutableDictionary
C # Dictionary

Declare to

create a map method:

// Thread-unsafe map 

// Create an empty map 
m1 = {} 

// Initialize the constant content 
m2 = {"name": "z"} 

// During initialization, the key can omit the string identifier (', ", \ `) 
m3 = {name:" z "} 

// At initialization,When key and variable name are the same, 
m4 = {m3} 

// thread-safe map

// Initialize a multi-thread-safe map 
m5 = new (sync.Map) 

// Initialize a thread-safe map with a limited number of keys and automatically eliminate unpopular keys 
m6 = new ("weak") 
m7 = new ("weak" , 2) // The number of keys is limited to a maximum of 2, triggering elimination when more than two 

// Initialize a thread-safe map with expired elimination mechanism 
m8 = new ("expire") 
m9 = new ("expire", 3) / The default expiration time of the / key is 3 seconds 
m10 = new ("expire", new ("weak")) // execute two elimination strategies at the same time 

In addition there are methods to generate

// from the string Convert to map 
m11 = JSON.parse (`{" name ":" zzz "}`) 

Assignment

can be done in the following ways

  • map variable name **. ** key = value
  • map variable name [key] = value

Example:

m = { 
name: "z", 
}; 
m.age = 18; // Set age 
m ["tall"] = 171 ;// Set tall 
key = "weight";
m [key] = 61.5; // Set weight 
print (m); 
```The 

above example will 

output

``` 
{"age": 18, "name": "z", "tall": 171, "weight ": 61.5} 

The value

can be obtained from the map in the following ways-

map variable name **. **key - map variable name [key] -{key1, key2} = Variable name inherits the

above environment:

print ("Name:", m ["name"]); 
print ("Height:", m.tall); 
{age, weight} = m; 
print (`Age: $ (age), Weight: $ (weight)`); 
```The 

above example will 

output```
Name: z 
Height: 171 
Age: 18, Weight: 61.5 

Optional

If the key does not exist when the chain tries to value the map, a Not In error will be thrown. At this time, there are two ways to judge and value

-has (map variable name, key) // return bool type -map variable name ?. key // If the key does not exist, return nil -map variable name ?[key variable]// If the key does not exist, return nil to

inherit the above environment:

print (has (m, "name")); // true 
print (m? .name); // z 
print (has (m, "gender")); // false 
print (m? .gender); // null 
print (m? ["score"]); // null 

Post-incremental increment and decrement increment

a key of the map, for example

m.age ++; // 19 
m ["age"] ++ // 20 
m.weight--; / / 60.5 
m [ "weight"] -; // 59.5 

If a thread safe map, count may be incremented to a non-existent Key

m.score ++; //. 1 
m ["score"] ++ // 2 

remove key

** Thread unsafe map ** delete method is as follows:

del (m, "age"); 

key = "score" ; 
del (m, key);

** The thread-safe Map ** deletion method is as follows:

m.Delete (" age "); 

key =" score "; 
m.Delete (" age "); 

Serialize the

following methods to ** to convert to a thread-safe string ** Map

  • the JSON.stringify (m);
  • string (m);

acquires a list of key

Object.keys (m) 

list acquisition value

Object.values (m)

traversing

@ mode. 1 for (K, V in m) { Print ( Key $ = {K}, the Value = '$ {V}) } ; // Method 2 Object.keys (m) .forEach (k => { print (Key = $ {k}, Value = $ (m [k]}) }); "`