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包