Dart by Example: Set

  1. main() {
  2. var medals = new Set();
  3. medals.add("gold");
  4. medals.add("silver");
  5. medals.add("bronze");
  6. // HashSets use the equality operator (==) to determine
  7. // if it already contains an item
  8. medals.add("gold");
  9. print('has gold? ${medals.contains("gold")}');
  10. print('has platinum? ${medals.contains("platinum")}');
  11. // Sets can be constructed from Iterables
  12. var meals = new Set.from(['breakfast', 'lunch', 'dinner']);
  13. // Some built-in features:
  14. print(medals.union(meals));
  15. print(medals.lookup("gold"));
  16. print(medals.lookup("platinum"));
  17. print(medals.difference(meals));
  18. }
  19.  
  1. $ dart set.dart
  2. has gold? true
  3. has platinum? false
  4. {gold, silver, bronze, breakfast, lunch, dinner}
  5. gold
  6. null
  7. {gold, silver, bronze}

by @jryanio | source | license