📜  Dart– 套装

📅  最后修改于: 2021-09-02 05:25:19             🧑  作者: Mango

Dart中的 Sets 是 List 中的一个特殊情况,其中所有输入都是唯一的,即它不包含任何重复输入。它也可以解释为具有唯一输入的无序数组。当我们想在单个变量中存储唯一值而不考虑输入顺序时,集合就派上用场了。这些集合是通过使用set关键字来声明的。

有两种方法可以做到:

var variable_name = {};
 
or,
 
Set  variable_name = {};

示例 1:以两种不同的方式声明 set。

Dart
// Dart program to show the Sets concept
void main()
{
  // Declaring set in First Way
  var gfg1 = {'GeeksForGeeks'}; 
    
  // Printing First Set
  print("Output of first set: $gfg1");
    
  // Declaring set in Second Way
  Set gfg2 = {'GeeksForGeeks'};  
    
  // Printing Second Set
  print("Output of second set: $gfg2");
}


Dart
// Dart Program to declare repeated value 
// in a set and a list and then 
// comparing it
  
void main()
{
  // Declaring list with repeated value
  var gfg = ['Geeks','For','Geeks'];
    
  // Printing List
  print("Output of the list is: $gfg");
    
  // Declaring set with repeated value
  var gfg1 = {'Geeks','For','Geeks'};  
    
  // Printing Set
  print("Output of the set is: $gfg1");
}


Dart
void main()
{
  // Declaring set with value
  var gfg = {'Hello Geek'};
    
  // Printing Set
  print("Value in the set is: $gfg");
    
  // Adding an element in the set
  gfg.add("GeeksForGeeks");
    
  // Printing Set
  print("Values in the set is: $gfg");
    
  // Adding multiple values to the set
  var geeks_name = {"Geek1","Geek2","Geek3"};
  gfg.addAll(geeks_name);
    
  // Printing Set
  print("Values in the set is: $gfg");
    
  // Getting element at Index 0
  var geek = gfg.elementAt(0);
    
  // Printing the element
  print("Element at index 0 is: $geek");
    
  // Counting the length of the set
  int l = gfg.length;
    
  // Printing length
  print("Length of the set is: $l");
    
  // Finding the element in the set
  bool check = gfg.contains("GeeksForGeeks");
    
  // Printing boolean value
  print("The value of check is: $check");
    
  // Removing an element from the set
  gfg.remove("Hello Geek");
    
  // Printing Set
  print("Values in the set is: $gfg");
    
  // Using forEach in set
  print(" ");
  print("Using forEach in set:");
  gfg.forEach((element) {
    if(element == "Geek1")
    {
      print("Found");
    }
    else
    {
      print("Not Found");
    }
  });
    
  // Deleting elements from set
  gfg.clear();
    
  // Printing set
  print("Values in the set is: $gfg");
}


Dart
// Converting Set to List in Dart
void main()
{
  // Declaring set with value
  var gfg = {'Hello Geek',"GeeksForGeeks","Geek1","Geek2","Geek3"};  
    
  // Printing values in set
  print("Values in set are:");
  print(gfg);
    
  print("");
    
  // Converting Set to List
  List gfg_list = gfg.toList();
    
  // Printing values of list
  print("Values in the list are:");
  print(gfg_list);
}


Dart
// Converting Set to Map in Dart
void main()
{
  // Declaring set 1 with value
  var gfg = {"GeeksForGeeks","Geek1","Geek2","Geek3"};
    
  var geeksforgeeks = gfg.map((value) {
    return 'mapped $value';
  });
  print("Values in the map:");
  print(geeksforgeeks);
      
}


Dart
// Set Operations in Dart
  
void main()
{
  // Declaring set 1 with value
  var gfg1 = {"GeeksForGeeks","Geek1","Geek2","Geek3"};
    
  // Printing values in set
  print("Values in set 1 are:");
  print(gfg1);
    
  print("");
    
  // Declaring set 2 with value
  var gfg2 = {"GeeksForGeeks","Geek3","Geek4","Geek5"};
    
  // Printing values in set
  print("Values in set 2 are:");
  print(gfg2);
    
  print("");
    
    
  // Finding Union
  print("Union of two sets is ${gfg1.union(gfg2)} \n");
    
  // Finding Intersection
  print("Intersection of two sets is ${gfg1.intersection(gfg2)} \n");
    
  // Finding Difference
  print("Difference of two sets is ${gfg2.difference(gfg1)} \n");
      
}


Dart
// Comparing more than 2 sets in Dart
  
void main()
{
  // Declaring set 1 with value
  var gfg1 = {"GeeksForGeeks","Geek1","Geek2","Geek3"};
    
  // Declaring set 2 with value
  var gfg2 = {"GeeksForGeeks","Geek3","Geek4","Geek5"};
    
  // Declaring set 3 with value
  var gfg3 = {"GeeksForGeeks","Geek5","Geek6","Geek7"};
    
  // Finding Union
  print("Union of two sets is ${gfg1.union(gfg2).union(gfg3)}\n");
    
  // Finding Intersection
  print("Intersection of two sets is ${gfg1.intersection(gfg2).intersection(gfg3)}\n");
    
  // Finding Difference
  print("Difference of two sets is ${gfg2.difference(gfg1).difference(gfg3)}\n");
}


输出:

Output of first set: {GeeksForGeeks}
Output of seccond set: {GeeksForGeeks}

示例 2:在集合和列表中声明重复值,然后进行比较。

Dart

// Dart Program to declare repeated value 
// in a set and a list and then 
// comparing it
  
void main()
{
  // Declaring list with repeated value
  var gfg = ['Geeks','For','Geeks'];
    
  // Printing List
  print("Output of the list is: $gfg");
    
  // Declaring set with repeated value
  var gfg1 = {'Geeks','For','Geeks'};  
    
  // Printing Set
  print("Output of the set is: $gfg1");
}

输出:

Output of the list is: [Geeks, For, Geeks]
Output of the set is: {Geeks, For}

注意:您可以看到,在 set 的情况下,重复的值被简单地忽略了。

在集合中添加元素:在集合中添加元素,我们使用“.add()”函数或“.addAll()”函数。但是您必须注意,如果您尝试使用这些函数添加重复值,那么它们也会在集合中被忽略。

// To add single value
variable_name.add(value);

// To add multiple value
variable_name.addAll(value1, value2, value3, ...valueN)the 

其他一些涉及 Sets 的函数如下:

S. No. Syntax Description
1. variable_name.elementAt(index); It returns the element at the corresponding index. The type of output depends on the type of set defined.
2. variable_name.length; It returns the length of the set. The output is of integer type.
3. variable_name.contains(element_name); It returns boolean value true if the element_name is present in the set else return false.
4. variable_name.remove(element_name); It deletes the element whose name is present inside it.
5. variable_name.forEach(…); It runs the command defined inside forEach() function for all the elements inside the set.
6. variable_name.clear(); It deletes all the element inside the set.

例子:

Dart

void main()
{
  // Declaring set with value
  var gfg = {'Hello Geek'};
    
  // Printing Set
  print("Value in the set is: $gfg");
    
  // Adding an element in the set
  gfg.add("GeeksForGeeks");
    
  // Printing Set
  print("Values in the set is: $gfg");
    
  // Adding multiple values to the set
  var geeks_name = {"Geek1","Geek2","Geek3"};
  gfg.addAll(geeks_name);
    
  // Printing Set
  print("Values in the set is: $gfg");
    
  // Getting element at Index 0
  var geek = gfg.elementAt(0);
    
  // Printing the element
  print("Element at index 0 is: $geek");
    
  // Counting the length of the set
  int l = gfg.length;
    
  // Printing length
  print("Length of the set is: $l");
    
  // Finding the element in the set
  bool check = gfg.contains("GeeksForGeeks");
    
  // Printing boolean value
  print("The value of check is: $check");
    
  // Removing an element from the set
  gfg.remove("Hello Geek");
    
  // Printing Set
  print("Values in the set is: $gfg");
    
  // Using forEach in set
  print(" ");
  print("Using forEach in set:");
  gfg.forEach((element) {
    if(element == "Geek1")
    {
      print("Found");
    }
    else
    {
      print("Not Found");
    }
  });
    
  // Deleting elements from set
  gfg.clear();
    
  // Printing set
  print("Values in the set is: $gfg");
}

输出:

Value in the set is: {Hello Geek}
Values in the set is: {Hello Geek, GeeksForGeeks}
Values in the set is: {Hello Geek, GeeksForGeeks, Geek1, Geek2, Geek3}
Element at index 0 is: Hello Geek
Length of the set is: 5
The value of check is: true
Values in the set is: {GeeksForGeeks, Geek1, Geek2, Geek3}
 
Using forEach in set:
Not Found
Found
Not Found
Not Found
Values in the set is: {}

在Dart中将集合转换为列表 Dart还为我们提供了将集合转换为列表的方法。为此,我们使用Dart的toList()方法。

List list_variable_name = set_variable_name.toList();

注意:它很有用,因为我们将获得的列表将包含唯一值且没有重复值。例子:

Dart

// Converting Set to List in Dart
void main()
{
  // Declaring set with value
  var gfg = {'Hello Geek',"GeeksForGeeks","Geek1","Geek2","Geek3"};  
    
  // Printing values in set
  print("Values in set are:");
  print(gfg);
    
  print("");
    
  // Converting Set to List
  List gfg_list = gfg.toList();
    
  // Printing values of list
  print("Values in the list are:");
  print(gfg_list);
}

输出:

Values in set are:
{Hello Geek, GeeksForGeeks, Geek1, Geek2, Geek3}

Values in the list are:
[Hello Geek, GeeksForGeeks, Geek1, Geek2, Geek3]

在Dart中将 Set 转换为 Map: Dart还为我们提供了将 Set 转换为 Map的方法。

例子:

Dart

// Converting Set to Map in Dart
void main()
{
  // Declaring set 1 with value
  var gfg = {"GeeksForGeeks","Geek1","Geek2","Geek3"};
    
  var geeksforgeeks = gfg.map((value) {
    return 'mapped $value';
  });
  print("Values in the map:");
  print(geeksforgeeks);
      
}

输出:

Values in the map:
(mapped GeeksForGeeks, mapped Geek1, mapped Geek2, mapped Geek3)

Set操作在Dart:有在设置三个操作的Dart:

例子:

Dart

// Set Operations in Dart
  
void main()
{
  // Declaring set 1 with value
  var gfg1 = {"GeeksForGeeks","Geek1","Geek2","Geek3"};
    
  // Printing values in set
  print("Values in set 1 are:");
  print(gfg1);
    
  print("");
    
  // Declaring set 2 with value
  var gfg2 = {"GeeksForGeeks","Geek3","Geek4","Geek5"};
    
  // Printing values in set
  print("Values in set 2 are:");
  print(gfg2);
    
  print("");
    
    
  // Finding Union
  print("Union of two sets is ${gfg1.union(gfg2)} \n");
    
  // Finding Intersection
  print("Intersection of two sets is ${gfg1.intersection(gfg2)} \n");
    
  // Finding Difference
  print("Difference of two sets is ${gfg2.difference(gfg1)} \n");
      
}

输出:

Values in set 1 are:
{GeeksForGeeks, Geek1, Geek2, Geek3}

Values in set 2 are:
{GeeksForGeeks, Geek3, Geek4, Geek5}

Union of two sets is {GeeksForGeeks, Geek1, Geek2, Geek3, Geek4, Geek5} 

Intersection of two sets is {GeeksForGeeks, Geek3} 

Difference of two sets is {Geek4, Geek5} 

在上面的代码中,我们还可以比较两个以上的集合:

例子:

Dart

// Comparing more than 2 sets in Dart
  
void main()
{
  // Declaring set 1 with value
  var gfg1 = {"GeeksForGeeks","Geek1","Geek2","Geek3"};
    
  // Declaring set 2 with value
  var gfg2 = {"GeeksForGeeks","Geek3","Geek4","Geek5"};
    
  // Declaring set 3 with value
  var gfg3 = {"GeeksForGeeks","Geek5","Geek6","Geek7"};
    
  // Finding Union
  print("Union of two sets is ${gfg1.union(gfg2).union(gfg3)}\n");
    
  // Finding Intersection
  print("Intersection of two sets is ${gfg1.intersection(gfg2).intersection(gfg3)}\n");
    
  // Finding Difference
  print("Difference of two sets is ${gfg2.difference(gfg1).difference(gfg3)}\n");
}

输出:

Union of two sets is {GeeksForGeeks, Geek1, Geek2, Geek3, Geek4, Geek5, Geek6, Geek7} 

Intersection of two sets is {GeeksForGeeks} 

Difference of two sets is {Geek4}