Dart Set集合

2019-10-16 22:06:56

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']='[email protected]'; 
   print('Map after adding  entries :${accounts}'); 
}

執行上面範例程式碼,得到以下結果 -

Map after adding entries :{email: [email protected], 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':'[email protected]'}); 
   print('Map after adding  entries :${accounts}'); 
}

執行上面範例程式碼,得到以下結果 -

Map after adding  entries :{email: [email protected], 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'] = '[email protected]'; 
   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: [email protected], dept: HR, name: Maxsu} 
Map after removing  entry :{email: [email protected], 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.. {}