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}");  

   // all elements are retrieved in the order in which they are inserted 
   for(var no in numberSet) { 
      print(no); 
   } 
}

执行上面示例代码,得到以下结果 -

100 
20 
5 
60 
70

示例:Set.from()

void main() { 
   Set numberSet = new Set.from([12,13,14]); 
   print("Default implementation :${numberSet.runtimeType}");  
   // all elements are retrieved in the order in which they are inserted 
   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']='Maxsu'; 
   accounts['email']='maxsu@xyz.com'; 
   print('Map after adding  entries :${accounts}'); 
}

执行上面示例代码,得到以下结果 -

Map after adding entries :{email: maxsu@xyz.com, dept: HR, name: Maxsu}

将多个值添加到HashMap

HashMap类从Map类继承addAll()函数,使用此函数可以一次添加多个值。

语法

HashMap.addAll(Iterable)

其中,Iterable表示要插入的值列表。

示例

import 'dart:collection'; 
main() { 
   var accounts = new HashMap(); 
   accounts.addAll({'dept':'HR','email':'maxsu@xyz.com'}); 
   print('Map after adding  entries :${accounts}'); 
}

执行上面示例代码,得到以下结果 -

Map after adding  entries :{email: maxsu@xyz.com, dept: HR}

从HashMap中删除值

remove()clear()函数用于从HashMap中删除条目。remove()函数传递一个代表要删除的条目的键。clear()函数用于从Map中删除所有条目。

示例

import 'dart:collection'; 
main() { 
   var accounts = new HashMap(); 
   accounts['dept'] = 'HR'; 
   accounts['name'] = 'Maxsu'; 
   accounts['email'] = 'maxsu@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: maxsu@xyz.com, dept: HR, name: Maxsu} 
Map after removing  entry :{email: maxsu@xyz.com, name: Maxsu} 
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集合 下一篇: Dart包