Dart-programming-collection-set

提供:Dev Guides
移動先:案内検索

Dartプログラミング-コレクションセット

Setは、各オブジェクトが1回しか出現できないオブジェクトのコレクションを表します。 dart:coreライブラリは、同じものを実装するSetクラスを提供します。

構文

Identifier = new Set()

OR

Identifier = new Set.from(Iterable)

ここで、 Iterable は、セットに追加する値のリストを表します。

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

次の output が生成されるはずです-

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

次の output が生成されるはずです-

12
13
14

Advanced Dart Collection─dart:コレクションライブラリ

dart:collectionライブラリは、Dartコレクションのさまざまな実装を可能にするクラスを提供します。 このセクションでは、次のトピックについて説明します。

  • ハッシュマップ
  • ハッシュセット
  • LinkedList
  • キュー

ハッシュマップ

HashMapは、Mapのハッシュテーブルベースの実装です。 HashMapのキーまたは値を反復処理する場合、特定の順序を期待することはできません。 同じための構文は以下のとおりです-

構文

Identifier= new HashMap()

次の例は、HashMapを実装する方法を示しています-

import 'dart:collection';
main() {
   var accounts = new HashMap();
   accounts['dept']='HR';
   accounts['name']='Tom';
   accounts['email']='[email protected]';
   print('Map after adding  entries :${accounts}');
}

次の output が生成されるはずです-

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

次の output が生成されるはずです-

Map after adding  entries :{email: [email protected], dept: HR}

HashMapから値を削除する

  • remove()および clear()*関数は、HashMapからエントリを削除するために使用されます。 * remove()*関数には、削除するエントリを表すキーが渡されます。 * clear()*関数は、マップからすべてのエントリを削除するために使用されます。

import 'dart:collection';
main() {
   var accounts = new HashMap();
   accounts['dept'] = 'HR';
   accounts['name'] = 'Tom';
   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}');
}

次の output が生成されるはずです-

Map after adding  entries :{email: [email protected], dept: HR, name: Tom}
Map after removing  entry :{email: [email protected], name: Tom}
Map after clearing entries :{}

ハッシュセット

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

次の output が生成されるはずです-

60
20
100
5
70

ハッシュセットへの複数の値の追加

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

次の output が生成されるはずです-

Default implementation :_HashSet
200
300
100

ハッシュセットから値を削除する

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

次の output が生成されるはずです-

Printing hashet.. {200, 300, 100}
Printing hashet.. {200, 300}
Printing hashet.. {}