Introduction
There are three sets in dart, namely list, set and map. Dart provides very useful methods for these three collections in the dart:core package, let's take a look.
Use of List
The first is the creation of the list. You can create an empty list or a list with values:
var emptyList =[];
var nameList = ['jack','mac'];
Use the List constructor to create:
var nameList = List.filled(2, 'max');
Add an element or list to the list:
nameList.add('tony');
nameList.addAll(['lili', 'bruce']);
Delete element:
nameList.removeAt(0);
nameList.clear();
Dart provides a sort method for list, sort(), sort can be followed by a comparison function to indicate who is in the front and who is in the back:
var names = ['jack', 'tony', 'max'];
fruits.sort((a, b) => a.compareTo(b));
Generics can also be used in the list to indicate the fixed types in the list:
var names = <String>[];
names.add('jack');
Use of Set
Set represents a collection of unique elements. But the difference between set and list is that set is unordered, so you can't use index to locate elements in set.
Take a look at the basic usage of set:
//创建一个空的set
var names = <String>{};
// 添加新的元素
names.addAll(['jack', 'tony', 'max']);
//删除元素
names.remove('jack');
Or use Set's constructor to construct Set:
var names = Set.from(['jack', 'tony', 'max']);
Determine whether the elements in the set exist:
assert(names.contains('jack'));
assert(names.containsAll(['jack', 'tony']));
Set also has an intersection operation, which is used to find the intersection of two sets:
var name1 = Set<String>();
name1.addAll(['jack', 'tony', 'max']);
var name2 = Set.from(['tony', 'bily']);
var intersection = name1.intersection(name2);
Use of Map
Map is a key and value data type, and it is also a very common data type in programs.
First look at how to create a Map:
// 创建map
var studentMap = {
'name': 'jack',
'age': '18',
'class': 'class one'
};
var teacherMap = Map();
var teacherMap2 = Map<String, String>();
Add and delete:
var studentMap =Map();
studentMap.putIfAbsent('name', ()=>'jack');
studentMap.remove('name');
To determine whether a key is included in the map, you can use containsKey():
assert(studentMap.containsKey('name'));
Common collection methods
The most common method in a collection is to determine whether the collection is empty:
assert(studentMap.isEmpty);
assert(studentMap.isNotEmpty);
If you want to perform a certain function operation on each element in the collection, you can use forEach():
var names = ['jack', 'bob', 'tom'];
names.forEach((name) => print('the name is $name'));
var nameMap = {};
nameMap.forEach((k, v) {
});
If it is a traversable object, there is a map method, which will return a new object:
var names = ['jack', 'bob', 'mark'];
var names2 = names.map((name) => name.toUpperCase());
Note that map returns an Iterable, which is calculated with a delay and will only be calculated when it is used.
If you want to calculate immediately, you can use map().toList() or map().toSet():
var names2 =
names.map((name) => name.toUpperCase()).toList();
Traversable objects can also be selected for conditions. For example, use where() to obtain all matching objects, use any() to determine whether there are matching objects in the collection, and use every() to determine whether all matching objects in the collection are matched.
var names = ['jack', 'bob', 'max'];
bool hasJack(String name) =>
name == 'jack';
var seleteJack =
names.where((name) => hasJack(name));
assert(names.any(hasJack));
assert(!names.every(hasJack));
Summarize
Set is a very common type in the process of programming, everyone must be familiar with it.
This article has been included in http://www.flydean.com/15-dart-collection/
The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!
Welcome to pay attention to my official account: "programs, those things", know the technology, know you better!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。