{"id":3578,"date":"2022-05-19T05:49:15","date_gmt":"2022-05-19T05:49:15","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=3578"},"modified":"2022-05-19T05:49:15","modified_gmt":"2022-05-19T05:49:15","slug":"dart-sets","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/05\/19\/dart-sets\/","title":{"rendered":"Dart Sets"},"content":{"rendered":"\n<p>The Dart Set is the unordered collection of the different values of the same type. It has much functionality, which is the same as an array, but it is unordered. Set doesn&#8217;t allow storing the duplicate values. The set must contain unique values.<\/p>\n\n\n\n<p>It plays an essential role when we want to store the distinct data of the same type into the single variable. Once we declare the type of the Set, then we can have an only value of the same type. The set cannot keep the order of the elements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Dart Initializing Set<\/h2>\n\n\n\n<p>Dart\u00a0provides the two methods to declare\/initialize an empty set. The set can be declared by using the {}\u00a0<strong>curly braces<\/strong>\u00a0proceeded by a type argument, or declare the variable type\u00a0<strong>Set<\/strong>\u00a0with curly braces {}. The syntax of declaring set is given below.<\/p>\n\n\n\n<p><strong>Syntax &#8211;<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var setName = &lt;type>{};  \nOr   \nSet&lt;type> setname = {};   <\/code><\/pre>\n\n\n\n<p>The setname refers to the name of the set variable, and type refers to the data type of the set.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Note &#8211; It should be remembered that the syntax of the set is much similar to the map literals. If we forget to define the type annotation with {} or with the variable it&#8217;s assigned to; then, Dart compiler will create Map object instead of Set.<\/h4>\n\n\n\n<p>Let&#8217;s have a look at the following example of set declaration &#8211;<\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main(){  \n   print(\"Initializing the Set\");  \n   var names = &lt;String>{\"James\",\"Ricky\", \"Devansh\",\"Adam\"};  \n   print(names);  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Initializing the Set\n{James, Ricky, Devansh, Adam}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Add Element into Set<\/h2>\n\n\n\n<p>The Dart provides the two methods&nbsp;<strong>add()<\/strong>&nbsp;and&nbsp;<strong>addAll()<\/strong>&nbsp;to insert an element into the given set. The&nbsp;<strong>add()<\/strong>&nbsp;method is used to add the single item into the given set. It can add one at a time when the&nbsp;<strong>addAll()<\/strong>&nbsp;method is used to add the multiple elements to an existing set. The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Set_name.add(&lt;value>);  \nOr   \nSet_name.addAll(val1,val2....valN) <\/code><\/pre>\n\n\n\n<p>Consider the following example &#8211;<\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main(){  \n   print(\"Insert element into the Set\");  \n   var names = {\"James\",\"Ricky\",\"Devansh\",\"Adam\"};   \n   \/\/ Declaring empty set  \n   var emp = &lt;String>{};  \n   emp.add(\"Jonathan\");  \n   print(emp);  \n     \n   \/\/ Adding multiple elements  \n   emp.addAll(names);  \n   print(emp);  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Insert element into the Set\n{Jonathan}\n{Jonathan, James, Ricky, Devansh, Adam}\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation &#8211;<\/strong><\/p>\n\n\n\n<p>We have declared two sets of&nbsp;<strong>names<\/strong>&nbsp;and&nbsp;<strong>emp.<\/strong>&nbsp;The set&nbsp;<strong>names<\/strong>&nbsp;consisted of few elements, while&nbsp;<strong>emp<\/strong>&nbsp;is an empty set. We added the single element &#8220;Jonathan&#8221; by using the&nbsp;<strong>add()<\/strong>&nbsp;method then; we called the addAll() method and passed another set&nbsp;<strong>names<\/strong>&nbsp;as an argument. It added the multiple values to the&nbsp;<strong>emp<\/strong>&nbsp;set.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Access the Set Element<\/h2>\n\n\n\n<p>Dart provides the&nbsp;<strong>elementAt()<\/strong>&nbsp;method, which is used to access the item by passing its specified index position. The set indexing starts from the 0 and goes up to size &#8211; 1, where size is the number of the element exist in the Set. It will throw an error if we enter the bigger index number than its size. The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Set_name.elementAt(index)  <\/code><\/pre>\n\n\n\n<p>Consider the following example.<\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main(){  \n   print(\"Access element from the Set\");  \n   var names = {\"James\",\"Ricky\",\"Devansh\",\"Adam\"};  \n   print(names);  \n     \n   var x = names.elementAt(3);  \n   print(x);  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Access element from the Set\n{James, Ricky, Devansh, Adam}\nAdam\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation &#8211;<\/strong><\/p>\n\n\n\n<p>In the above example, we have set names. We applied the&nbsp;<strong>elementAt()<\/strong>&nbsp;method and passed index position 3 as an argument. We created a variable&nbsp;<strong>x,<\/strong>&nbsp;which holds the assessed value, and then we printed the result.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Dart Finding Element in Set<\/h2>\n\n\n\n<p>Dart provides the&nbsp;<strong>contains()<\/strong>&nbsp;method, which is used to find an element in the set. It accepts the single item as an argument ad return the result in Boolean type. If the given element present in the set, it returns true otherwise false. The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>set_name.contains(value);  <\/code><\/pre>\n\n\n\n<p><strong>Example &#8211;<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main()  {  \n  \n  print(\"Example - Find Element in the given Set\");  \n  var names = &lt;String>{\"Peter\",\"John\",\"Ricky\",\"Devansh\",\"Finch\"};  \n  \n  if(names.contains(\"Ricky\")){  \n     print(\"Element Found\");  \n  }  \n  \n  else {  \n    print(\"Element not found\");  \n }  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Example - Find Element in the given Set\nElement Found\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation &#8211;<\/strong><\/p>\n\n\n\n<p>In the above program, to find the element in the given set, we called&nbsp;<strong>contains()<\/strong>&nbsp;method and passed value &#8220;Ricky&#8221; as an argument. We used the conditional statement to find out whether an element belongs to the given set or not. The given element present in the set then condition became true, it printed if block statement.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Note &#8211; We will learn conditional statement in the next section.<\/h4>\n\n\n\n<h2 class=\"wp-block-heading\">Dart Remove Set Element<\/h2>\n\n\n\n<p>The&nbsp;<strong>remove()<\/strong>&nbsp;method is used to eliminate or remove an element from the given set. It takes the value as an argument; the value is to be removed in the given set. The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax &#8211;<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>set_names.contains(value)  <\/code><\/pre>\n\n\n\n<p><strong>Example &#8211;<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main()  {  \n  \n    print(\"Example - Remove Element in the given Set\");  \n    var names = &lt;String>{\"Peter\", \"John\", \"Ricky\", \"Devansh\", \"Finch\"};  \n    print(\"Before remove : ${names}\");  \n  \n     names.remove(\"Peter\");  \n     print(\"After remove  :  ${names}\");  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Example - Remove Element in the given Set\nBefore remove : {Peter, John, Ricky, Devansh, Finch}\nAfter remove  :  {John, Ricky, Devansh, Finch}\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation &#8211;<\/strong><\/p>\n\n\n\n<p>In the above program, we removed the &#8220;Peter&#8221; from the given set by using the&nbsp;<strong>remove()<\/strong>&nbsp;method. It returned the newly modified set object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Dart Iterating Over a Set Element<\/h2>\n\n\n\n<p>In Dart, the set element can be iterated using the&nbsp;<strong>forEach<\/strong>&nbsp;method as following &#8211;<\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main()  {  \n    print(\"Example - Remove Element in the given Set\");  \n    var names = &lt;String>{\"Peter\",\"John\",\"Ricky\",\"Devansh\",\"Finch\"};  \n  \n    names.forEach((value) {  \n        print('Value:  $value');  \n     });  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Example - Remove Element in the given Set\nValue:  Peter\nValue:  John\nValue:  Ricky\nValue:  Devansh\nValue:  Finch\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Dart Remove All Set Element<\/h2>\n\n\n\n<p>We can remove entire set element by using the&nbsp;<strong>clear()<\/strong>&nbsp;methods. It deletes or removes all elements to the given set and returns an empty set. The syntax is as follow-<\/p>\n\n\n\n<p><strong>Syntax &#8211;<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>set_name.clear();  <\/code><\/pre>\n\n\n\n<p><strong>Example &#8211;<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main()  {  \n    print(\"Example - Remove All Element to the given Set\");  \n    var names = &lt;String>{\"Peter\",\"John\",\"Ricky\",\"Devansh\",\"Finch\"};  \n      \n    names.clear();  \n    print(names);  \n  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Example - Remove All Element to the given Set\n{Peter, John, Ricky, Devansh, Finch}\n{}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">TypeCast Set to List<\/h2>\n\n\n\n<p>The Set object can convert into the List Object using the&nbsp;<strong>toList()<\/strong>&nbsp;method. The syntax is as follows.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Note &#8211; The type of List must be the same as the type of Set.<\/h4>\n\n\n\n<p><strong>Syntax &#8211;<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;type> &lt;list_name> = &lt;set_name>. toList();  <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Dart Set Operations<\/h2>\n\n\n\n<p>Dart Set provides the facility to perform following set operations. These operations are given below.<\/p>\n\n\n\n<p><strong>Union &#8211;<\/strong>&nbsp;The union is set to combine the value of the two given sets a and b.<\/p>\n\n\n\n<p><strong>Intersection &#8211;<\/strong>&nbsp;The intersection of the two set a and b returns all elements, which is common in both sets.<\/p>\n\n\n\n<p><strong>Subtracting &#8211;<\/strong>&nbsp;The subtracting of two sets a and b (a-b) is the element of set b is not present in the set a.<\/p>\n\n\n\n<p>Let&#8217;s understand the following example.<\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-sets#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main()  {  \n  \n    var x = &lt;int>{10,11,12,13,14,15};  \n    var y = &lt;int>{12,18,29,43};  \n    var z = &lt;int>{2,5,10,11,32};  \n    print(\"Example - Set Operations\");  \n      \n    print(\"x union y is -\");  \n    print(x.union(y));  \n  \n    print(\"x intersection y is - \");  \n    print(x.intersection(y));  \n      \n    print(\"y difference z is - \");  \n     print(y.difference(z));   \n      \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Example - Set Operations\nx union y is -\n{10, 11, 12, 13, 14, 15, 18, 29, 43}\nx intersection y is -\n{12}\ny difference z is -\n{12, 18, 29, 43} \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Dart Set Properties<\/h2>\n\n\n\n<p>The few properties of the Dart set as follows.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><th>Properties<\/th><th>Explanations<\/th><\/tr><tr><td>first<\/td><td>It is used to get the first element in the given set.<\/td><\/tr><tr><td>isEmpty<\/td><td>If the set does not contain any element, it returns true.<\/td><\/tr><tr><td>isNotEmpty<\/td><td>If the set contains at least one element, it returns true<\/td><\/tr><tr><td>length<\/td><td>It returns the length of the given set.<\/td><\/tr><tr><td>last<\/td><td>It is used to get the last element in the given set.<\/td><\/tr><tr><td>hashcode<\/td><td>It is used to get the hash code for the corresponding object.<\/td><\/tr><tr><td>Single<\/td><td>It is used to check whether a set contains only one element.<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>The Dart Set is the unordered collection of the different values of the same type. It has much functionality, which is the same as an array, but it is unordered. Set doesn&#8217;t allow storing the duplicate values. The set must contain unique values. It plays an essential role when we want to store the distinct [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[917],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3578"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=3578"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3578\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=3578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=3578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=3578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}