preface
This chapter introduces Map objects, DART: Convert, MapEntry, Javascript Map, and self-balancing binary trees
Video address: Portal
Map
A Map object is a simple key/value pair, and a Map is a dynamic collection. In other words, Maps can grow and shrink at run time.
You can declare a Map in two ways
- Use the Map constructor
- Use the Map literal
Constructor declaration
Map objects can be declared using the constructor as follows
Literal declaration
Map objects are declared with literals like this:
Modify the operating
Constructor initialization:
Either of the above declarations can initialize a Map object as follows
In a Map key-value pair, the “key”, i.e., K, can be of any type (it can be called an object here, since all types are objects), including NULL.
As shown above, we assign two types of key, Map object type and NULL. It’s all ok.
You might want to take a look at var ahead of time. Remember that you used var to declare variables in the initial section on var. Type inference is performed during Dart compilation, and the same is true for Map types. But it’s a little different because Map objects have two types: key and value
Take a look at this example:
Dart will infer that the type of A is Map<String, String>, and if you try to add an error type to the Map above, either the profiler or the runtime will raise an error. The rest of the knowledge points are covered in the generics section. Let’s know in advance.
Common properties and methods
Map.from()
To clone a Map, use map.from () to achieve only shallow cloning.
For deep cloning, we can simply handle it as follows:
dart: convert
Decoding (JSON String – > Object)
Int, double, String, bool, null, List, Map (with String keys)
Decoding (utf-8)
Code (utf-8)
fromIterable()
Generate a Map object from Iterable
fromIterables()
Generate a Map object from two Iterable types, one for key and one for value. The length of two Iterable types must be the same.
keys
Retrieve all keys. In terms of the List object theory in the last video, the keys returned are Iterable, not List.
values
Get all values and return Iterable
isEmpty
Check whether the Map object is empty
remove()
Delete the data of the specified key
addAll()
Merges the specified Map object
containsValue()
Bool indicates whether a value is specified in a Map object
containsKey()
Bool specifies whether a key is specified in a Map. Bool is returned
length
Returns the number of key-value pairs in the map
forEach()
Loop the key and value pairs in the map. The internal function parameters are key and value respectively. The function is void and has no return value
You cannot delete or modify the value of a key in a forEach loop
updateAll()
Update all values according to function rules
map
Grammar:
Map () traverses each key-value pair, making changes to key and value according to the parameter function, and converting them to other maps. The Map of the transformation can be of another type.
MapEntry
Creates a key-value pair, or the representation of a key-value pair. So let’s just define the concept. A Map object is a set of key and value pairs. It’s a set. MapEntry indicates a key-value pair.
Of course, MapEntry has its own attributes and methods: key, value, and so on.
Multiple mapentries are entries. Let’s look at the syntax
Entries are of type Iterable. A single MapEntry makes little sense. Multiple mapentries must be of type Iterable. This makes it easier to iterate.
fromEntries()
Map objects can also be generated from multiple MapEntries
If Iterable is cast to Map, then forEach is Iterable.
Yes, but the iteration order is conceptually different
List: Iterates in index order.
The Map:
- A normal hashMap is unordered
- The LinkedHashMap iteration order is the key insertion order
- SplayTreeMap self balanced binary tree – stretch tree 🌲
The iteration order of Map objects falls into the second category.
Self-balancing binary tree
This thing is not good baidu Encyclopedia, directly watch the video: portal
So what does the SplayTreeMap in the Dart look like:
SplayTreeMap is a good choice for frequently stored and accessed data, such as caches. The reason is that they use tree rotation to tune an element to the root for more frequent access. Performance comes from tree self-optimization. That is, frequently accessed elements are moved closer to the top. However, it makes little sense to use SplayTreeMap if you also have frequent access to the tree.
For example, a modem router receives network packets at a very high rate. The modem must decide which packet goes on which line. This can be done using a map, where the key is IP and the value is the destination line. For this case, SplayTreeMap is a good choice because most IP addresses will be used multiple times, so they can be found at the root of the tree.
javascript Map
Object
Properties are identified using key values
An Object in javascript is essentially a collection of key-value pairs,
Keys can only be strings or Symbol values. A key of type Symbol. In the last lecture we talked about a Symbol.
This in itself can be very limiting. So to solve this problem, ES6 provides a Map data structure. It is similar to an object in that it is also a collection of key-value pairs, but the range of “keys” is not limited to strings, and all types of values (including objects) can be used as keys. That is, the Object structure provides the string – value correspondence, and the Map structure provides the value – value correspondence. If you need a “key-value pair” data structure, Map is more appropriate than Object.
Map
Javascript Map is more familiar to everyone, here to explain a little bit.
The map. keys returns the iteration protocol in javascript. The keys in the Dart Map object returned as Iterable, which corresponds to the Iterable section.
The traversal order of a Map in javascript is the insert order.
END
This chapter introduces Map objects, DART: Convert, MapEntry, Javascript Map, and self-balancing binary trees.