Collection types commonly used in Dart
- List: list, ordered collection, data can be modified. List in Dart corresponds to an array, and elements can be accessed through indexes.
- Set: collection, unordered, non-repeatable. Often used to filter duplicate data.
- Map: Mapping, a collection of key-value pairs. Keys must be unique, values can be repeated. Commonly used to store linked data.
- Queue: Queue, a collection of first-in-first-out.
example
List:
var list = [1, 2, 3];
list.add(4); // [1, 2, 3, 4]
list[0]; // 1
Set:
var set = {
1, 2, 3};
set.add(2); // {1, 2, 3} 不可重复
set.add(4); // {1, 2, 3, 4}
Map:
var map = {
'a': 1, 'b': 2};
map['a']; // 1
map['c'] = 3; // {'a': 1, 'b': 2, 'c': 3}
Queue:
var queue = Queue();
queue.add(1);
queue.add(2);
queue.removeFirst(); // 1, 返回第一个元素
queue.first; // 2, 获取第一个元素
In addition, Dart also provides collection types such as UnmodifiableListView, SplayTreeSet, and LinkedHashMap.
Dart's collection classes all implement the Iterable interface, so many common methods can be used, such as forEach(), map(), where(), reduce(), etc.
This makes Dart's collection operations very powerful and flexible.
collection of common methods
The collection types in Dart all implement the Iterable interface, so they have many common methods, such as:
- forEach(): used to traverse each element in the collection, no return value
- map(): converts each element into a new element and returns the converted collection
- where(): Filter the elements in the collection and return the set of elements that meet the conditions
- reduce(): Combines elements in a collection to return a single result
List example
Each method is explained in detail with examples below:
forEach():
var list = [1, 2, 3];
list.forEach(print); // 1 2 3
map():
map() is a very useful Dart collection operation that allows us to convert elements in a collection into a new form according to a transformation function, which is widely used in data processing and mapping.
This is a common means of mapping database query results to Dart model objects.
var list = [1, 2, 3];
var doubled = list.map((x) => x * 2);
print(doubled); // [2, 4, 6]
where():
var list = [1, 2, 3, 4];
var even = list.where((x) => x % 2 == 0);
print(even); // [2, 4]
reduce():
reduce() is an extremely useful collection method that
combines elements in a collection into a single result. It combines elements by executing a function that takes two arguments and returns a new combined result.
Its syntax is:
someCollection.reduce(combineFunction)
- someCollection is the collection to be reduced, which can be List, Set, etc.
- combineFunction is a function that combines two elements and returns the result
- reduce() returns the final single result.
For example, array summation can use reduce():
var list = [1, 2, 3];
var sum = list.reduce((a, b) => a + b);
print(sum); // 6
Here combineFunction is (a, b) => a + b, it will first calculate with a = 1, b = 2, and get the result 3; then a =
3, b = 3 to get the result 6, which is the final return of reduce() the result of.
You can also use reduce() to find the maximum value:
var list = [5, 3, 7, 4];
var max = list.reduce((a, b) => a > b ? a : b);
print(max); // 7
Here combineFunction compares a and b, returns the larger one, and finally gets the maximum value of 7 after multiple executions.
So, in summary, .reduce() performs a combineFunction to combine the collection elements in pairs, and finally get a single result.
This is a very efficient and compact way of implementing array summaries.
It is often used for sums, max/min, products, etc. It is not limited to numerical values, but can also be used for concatenation of strings, etc.
These methods make Dart's collection operations extremely powerful and flexible:
- We can easily iterate and filter collections
- Maps and converts collection elements to new elements
- Summary operations such as summation and maximum value are realized through reduce(),
and these methods are Iterable methods, so all collection types such as List, Set, and Map share these functions. They can help us easily process and transform data, and implement various complex business logics.
Set example
Let's look at a few examples of using Set:
forEach(): traverse each element in the Set
var set = {
'a', 'b', 'c'};
set.forEach(print);
// a
// b
// c
map(): convert each element into a new element, return Set
var set = {
1, 2, 3};
var doubled = set.map((x) => x * 2);
print(doubled); // {2, 4, 6}
where(): Filter the elements in the Set and return the qualified element Set
var set = {
1, 2, 3, 4};
var even = set.where((x) => x % 2 == 0);
print(even); // {2, 4}
reduce(): Performs a combined operation on the elements in the Set and returns a result
var set = {
'a', 'b', 'c'};
var concat = set.reduce((x, y) => x + y);
print(concat); // abc
The above examples show how to use these common collection methods with Dart's Set type.
Although Set is unordered and non-repeatable, it also has a powerful collection function, which allows us to easily:
- Iterate over all elements in the Set
- Filter and transform elements in a Set
- Combining the elements of Set through reduce()
In fact, since Set implements the Iterable interface, it has exactly the same collection methods as List.