Dart 编程 - 集合 Set
Set 表示对象集合,其中每个对象只能出现一次。dart:core 库提供了 Set 类来实现相同的功能。
语法
Identifier = new Set()
或
Identifier = new Set.from(Iterable)
其中,Iterable 表示要添加到 Set 的值列表。
示例
void main() {
Set numberSet = new Set();
numberSet.add(100);
numberSet.add(20);
numberSet.add(5);
numberSet.add(60);
numberSet.add(70);
print("Default implementation :${numberSet.runtimeType}");
// 所有元素都按照插入的顺序检索
for(var no in numberSet) {
print(no);
}
}
它应该产生以下输出 −
100 20 5 60 70
Illustration: Set.from()
void main() {
Set numberSet = new Set.from([12,13,14]);
print("Default implementation :${numberSet.runtimeType}");
// 所有元素都按照插入的顺序检索
for(var no in numberSet) {
print(no);
}
}
它应该产生以下输出 −
12 13 14
高级 Dart 集合 ─ dart: collection 库
dart:collection 库提供了支持各种 Dart 集合实现的类。我们将在本节中讨论以下主题。
- HashMap
- HashSet
- LinkedList
- Queue
HashMap
HashMap 是基于哈希表的 Map 实现。当您遍历 HashMap 的键或值时,您不能期望特定的顺序。其语法如下所示 −
语法
Identifier= new HashMap()
示例
以下示例展示了如何实现 HashMap −
import 'dart:collection';
main() {
var accounts = new HashMap();
accounts['dept']='HR';
accounts['name']='Tom';
accounts['email']='tom@xyz.com';
print('Map after adding entries :${accounts}');
}
它应该产生以下输出 −
Map after adding entries :{email: tom@xyz.com, dept: HR, name: Tom}
向 HashMap 添加多个值
HashMap 类从 Map 类继承了 addAll() 函数。此函数允许一次性添加多个值。
语法
HashMap.addAll(Iterable)
其中,Iterable 表示要插入的值列表。
示例
import 'dart:collection';
main() {
var accounts = new HashMap();
accounts.addAll({'dept':'HR','email':'tom@xyz.com'});
print('Map after adding entries :${accounts}');
}
它应该产生以下输出 −
Map after adding entries :{email: tom@xyz.com, dept: HR}
从 HashMap 中删除值
remove() 和 clear() 函数用于从 HashMap 中删除条目。remove() 函数传递一个表示要删除的条目的键。clear() 函数用于从 Map 中删除所有条目。
示例
import 'dart:collection';
main() {
var accounts = new HashMap();
accounts['dept'] = 'HR';
accounts['name'] = 'Tom';
accounts['email'] = 'tom@xyz.com';
print('Map after adding entries :${accounts}');
accounts.remove('dept');
print('Map after removing entry :${accounts}');
accounts.clear();
print('Map after clearing entries :${accounts}');
}
它应该产生以下输出 −
Map after adding entries :{email: tom@xyz.com, dept: HR, name: Tom}
Map after removing entry :{email: tom@xyz.com, name: Tom}
Map after clearing entries :{}
HashSet
HashSet 是基于无序哈希表的 Set 实现。其语法为 −
语法
Identifier = new HashSet()
可以使用 add() 函数来填充 HashSet 实例。
示例
import 'dart:collection';
void main() {
Set numberSet = new HashSet();
numberSet.add(100);
numberSet.add(20);
numberSet.add(5);
numberSet.add(60);
numberSet.add(70);
print("Default implementation :${numberSet.runtimeType}");
for(var no in numberSet){
print(no);
}
}
它应该产生以下输出 −
60 20 100 5 70
向 HashSet 添加多个值
addAll() 函数允许向 HashSet 添加多个值。以下示例说明了相同的 −
示例
import 'dart:collection';
void main() {
Set numberSet = new HashSet();
numberSet.addAll([100,200,300]);
print("Default implementation :${numberSet.runtimeType}");
for(var no in numberSet){
print(no);
}
}
它应该产生以下输出 −
Default implementation :_HashSet 200 300 100
从 HashSet 中删除值
remove() 函数删除传递给它的值。clear() 函数删除 HashSet 中的所有条目。
示例
import 'dart:collection';
void main() {
Set numberSet = new HashSet();
numberSet.addAll([100,200,300]);
print("Printing hashet.. ${numberSet}");
numberSet.remove(100);
print("Printing hashet.. ${numberSet}");
numberSet.clear();
print("Printing hashet.. ${numberSet}");
}
它应该产生以下输出 −
Printing hashet.. {200, 300, 100}
Printing hashet.. {200, 300}
Printing hashet.. {}
dart_programming_collection.html

