LoginSignup
8
4

More than 5 years have passed since last update.

Dart Collections -- maps, lists,queues, sets

Last updated at Posted at 2018-03-15

Collectionsとは

objectを格納するAPI
- maps
- lists
- queues
- sets
の四つがdartには用意されている

maps

keyとvalueを持っており,keyによって検索も可能。keyはユニークでなければならなく、nullを持つことはできないがどんなオブジェクトも代入可能

map
var gifts = {
  // Key:    Value
  'first': 'partridge',
  'second': 'turtledoves',
  'fifth': 'golden rings'
};

lists

順番があり、indexから検索、追加、削除が可能

lists
var fruits = ['apples', 'oranges', 'bananas'];
fruits[0]; // apples
fruits.add('pears');
fruits.length == 4;
fruits.where((f) => f.startsWith('a')).toList();

queues

queuesはlistsと同じく順番があり、最初と最後に追加もしくは削除ができる。しかしindexがないため真ん中の要素は触ることができない

queues
//import 'dart:collection';

var workOrders = new Queue();
workOrders.addFirst('B23432');//{B23432}
workOrders.addFirst('J78424');//{J78424, B23432}
var order = workOrders.removeFirst();
//order:J78424 wordOrders:{B23432}
workOrders.addLast('Z94093');//{B23432, Z94093}
workOrders.removeLast();//{B23432}

sets

要素の順序はないが、要素は必ず被らなく同じものは入ることができない
要素の順序は必要ないが、要素は必ず被らないので、同じものを入れたくない時に使う

sets
main() {
  var emails = new Set();
  emails.add('bob@example.com');
  emails.add('john@example.com');
  emails.add('bob@example.com');
  emails.length == 2;
  print(emails); //{bob@example.com, john@example.com}
  var otherEmails = new Set();
  otherEmails.add('bob@example.com');
  otherEmails.add('joe@example.com');
  var inBoth = emails.intersection(otherEmails);
  print(inBoth);// {bob@example.com}
}

同じオブジェクトが飛んできても何も起こらない

8
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4