상세 컨텐츠

본문 제목

Dart - Set

Dart

by techbard 2024. 1. 4. 22:08

본문

반응형
void main(List<String> args) {
  var uniqueNums = {1, 1, 2, 2, 3, 3, 4, 5};
  print(uniqueNums);
  print(uniqueNums.contains(1));

  // Difference In Set
  Set<String> fruits1 = {"Apple", "Orange", "Mango"};
  Set<String> fruits2 = {"Apple", "Grapes", "Banana"};
  final differenceSet = fruits1.difference(fruits2);
  print(differenceSet);

  // Intersection Method In Dart
  Set<String> fruits3 = {"Apple", "Orange", "Mango"};
  Set<String> fruits4 = {"Apple", "Grapes", "Banana"};
  final intersectionSet = fruits3.intersection(fruits4);
  print(intersectionSet);
}

// 결과
{1, 2, 3, 4, 5}
true
{Orange, Mango}
{Apple}

 

void main(List<String> args) {
  var uniqueNums = {1, 1, 2, 2, 3, 3, 4, 5};
  print(uniqueNums);
  print(uniqueNums.contains(1));

  // Difference In Set
  Set<String> fruits1 = {"Apple", "Orange", "Mango"};
  Set<String> fruits2 = {"Apple", "Grapes", "Banana"};
  final differenceSet = fruits1.difference(fruits2);
  print(differenceSet);

  // Intersection Method In Dart
  Set<String> fruits3 = {"Apple", "Orange", "Mango"};
  Set<String> fruits4 = {"Apple", "Grapes", "Banana"};
  final intersectionSet = fruits3.intersection(fruits4);
  print(intersectionSet);

  Set<String> colors1 = {'red', 'yellow', 'blue'};

  Set<String> color2 = {};
  color2.add('red');
  color2.add('yellow');
  color2.add('blue');

  Set<String> set1 = {}; // Empty Set
  Set<String> set2 = {'Aa', 'Bb', 'Cc'}; // A Set with String values
  var set3 = <int>{}; // Empty Set
  var set4 = <int>{2, 4, 6, 1}; // A Set with int values
  var set5 = {}; // Is not a Set, set5 has type Map

  print(set5.runtimeType);

  // set + list
  Set<String> set = {'A', 'B', 'C'};
  var list = ['D', 'E'];
  set.addAll(list);
  print(set);

  // Modifying elements
  //
  // Unlike a List, you cannot directly change elements in a Set
  // because each element must remain unique.
  // If you want to update an element,
  // you need to remove the old one and add the new value.
}

// 결과
{1, 2, 3, 4, 5}
true
{Orange, Mango}
{Apple}
_Map<dynamic, dynamic>
{A, B, C, D, E}

 

// Accessing an element by index
// Unlike a list, you cannot access an element at an index
// using square brackets [].
// Instead, you can use the elementAt() method like this:

/*
A set is a collection of unique elements. A set doesn’t maintain the order of elements.
Use the add() method to add an element to a set.
Use the addAll() method to add elements from a list to a set.
Use the remove() method to remove an element from a set.
Use the intersection() method to find the intersection of two sets.
Use the union() method to find the union of two sets.
Use the for-in to iterate over elements of a set.
*/

void main(List<String> args) {
  var ratingsMap = {'one': 1, 'two': 2, 'three': 3};
  print(ratingsMap.runtimeType);

  var ratingsSet = {1, 2, 3};
  print(ratingsSet.runtimeType);

  // 같은 curly braces를 사용하는데 : 에 의한 구분이 들어 있다면 Map
  // : 이 없다면 그냥 set collection 이다.

  print("first: ${ratingsSet.elementAt(0)}");
  print("second: ${ratingsSet.elementAt(1)}");
  print("third: ${ratingsSet.elementAt(2)}");

  // Checking the existence of elements
  print(ratingsSet.contains(1));
}

// 결과
_Map<String, int>
_Set<int>
first: 1
second: 2
third: 3
true

 

void main(List<String> args) {
  var c = {1, 3}.union({3, 5}); // {1, 3, 5}

  var d = {1, 3}.intersection({3, 5}); // {3}

  print(c);
  print(d);

  var result = c.difference(d); // {1, 5}
  print(result);
}

// 결과
{1, 3, 5}
{3}
{1, 5}

 

void main(List<String> args) {
  var euLangs = {'Russian', 'German', 'French', 'English'};

  print(euLangs.elementAt(2));
  print(euLangs.length);

  var afLangs = {'Swahili', 'Zulu', 'Arabic', 'French'};

  print(afLangs.union(euLangs)); // 합집합
  print(afLangs.intersection(euLangs)); // 교집합
  print(afLangs.difference(euLangs)); // 차집합
}

// 결과
French
4
{Swahili, Zulu, Arabic, French, Russian, German, English}
{French}
{Swahili, Zulu, Arabic}
반응형

관련글 더보기

댓글 영역