Dart Sets

 

Dart Sets

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't allow storing the duplicate values. The set must contain unique values.

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.

Dart Initializing Set

Dart

provides the two methods to declare/initialize an empty set. The set can be declared by using the {} curly braces proceeded by a type argument, or declare the variable type Set with curly braces {}. The syntax of declaring set is given below.

  1. var setName = <type>{};  
  2. Or   
  3. Set<type> setname = {};   

The setname refers to the name of the set variable, and type refers to the data type of the set.

Note - 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's assigned to; then, Dart compiler will create Map object instead of Set.

Let's have a look at the following example of set declaration -

Example -

  1. void main(){  
  2.    print("Initializing the Set");  
  3.    var names = <String>{"James","Ricky""Devansh","Adam"};  
  4.    print(names);  
  5. }  

Output:

Initializing the Set
{James, Ricky, Devansh, Adam}

Add Element into Set

The Dart provides the two methods add() and addAll() to insert an element into the given set. The add() method is used to add the single item into the given set. It can add one at a time when the addAll() method is used to add the multiple elements to an existing set. The syntax is given below.

Syntax:

  1. Set_name.add(<value>);  
  2. Or   
  3. Set_name.addAll(val1,val2....valN)  

Consider the following example -

  1. void main(){  
  2.    print("Insert element into the Set");  
  3.    var names = {"James","Ricky","Devansh","Adam"};   
  4.    // Declaring empty set  
  5.    var emp = <String>{};  
  6.    emp.add("Jonathan");  
  7.    print(emp);  
  8.      
  9.    // Adding multiple elements  
  10.    emp.addAll(names);  
  11.    print(emp);  
  12. }  

Output:

Insert element into the Set
{Jonathan}
{Jonathan, James, Ricky, Devansh, Adam}

Explanation -

We have declared two sets of names and emp. The set names consisted of few elements, while emp is an empty set. We added the single element "Jonathan" by using the add() method then; we called the addAll() method and passed another set names as an argument. It added the multiple values to the emp set.

Access the Set Element

Dart provides the elementAt() 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 - 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.

  1. Set_name.elementAt(index)  

Consider the following example.

Example -

  1. void main(){  
  2.    print("Access element from the Set");  
  3.    var names = {"James","Ricky","Devansh","Adam"};  
  4.    print(names);  
  5.      
  6.    var x = names.elementAt(3);  
  7.    print(x);  
  8. }  

Output:

Access element from the Set
{James, Ricky, Devansh, Adam}
Adam

Explanation -

In the above example, we have set names. We applied the elementAt() method and passed index position 3 as an argument. We created a variable x, which holds the assessed value, and then we printed the result.

Dart Finding Element in Set

Dart provides the contains() 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.

Syntax:

  1. set_name.contains(value);  

Example -

  1. void main()  {  
  2.   
  3.   print("Example - Find Element in the given Set");  
  4.   var names = <String>{"Peter","John","Ricky","Devansh","Finch"};  
  5.   
  6.   if(names.contains("Ricky")){  
  7.      print("Element Found");  
  8.   }  
  9.   
  10.   else {  
  11.     print("Element not found");  
  12.  }  
  13. }  

Output:

Example - Find Element in the given Set
Element Found

Explanation -

In the above program, to find the element in the given set, we called contains() method and passed value "Ricky" 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.

Note - We will learn conditional statement in the next section.

Dart Remove Set Element

The remove() 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.

Syntax -

  1. set_names.contains(value)  

Example -

  1. void main()  {  
  2.   
  3.     print("Example - Remove Element in the given Set");  
  4.     var names = <String>{"Peter""John""Ricky""Devansh""Finch"};  
  5.     print("Before remove : ${names}");  
  6.   
  7.      names.remove("Peter");  
  8.      print("After remove  :  ${names}");  
  9. }  

Output:

Example - Remove Element in the given Set
Before remove : {Peter, John, Ricky, Devansh, Finch}
After remove  :  {John, Ricky, Devansh, Finch}

Explanation -

In the above program, we removed the "Peter" from the given set by using the remove() method. It returned the newly modified set object.

Dart Iterating Over a Set Element

In Dart, the set element can be iterated using the forEach method as following -

Example -

  1. void main()  {  
  2.     print("Example - Remove Element in the given Set");  
  3.     var names = <String>{"Peter","John","Ricky","Devansh","Finch"};  
  4.   
  5.     names.forEach((value) {  
  6.         print('Value:  $value');  
  7.      });  
  8. }  

Output:

Example - Remove Element in the given Set
Value:  Peter
Value:  John
Value:  Ricky
Value:  Devansh
Value:  Finch

Dart Remove All Set Element

We can remove entire set element by using the clear() methods. It deletes or removes all elements to the given set and returns an empty set. The syntax is as follow-

Syntax -

  1. set_name.clear();  

Example -

  1. void main()  {  
  2.     print("Example - Remove All Element to the given Set");  
  3.     var names = <String>{"Peter","John","Ricky","Devansh","Finch"};  
  4.       
  5.     names.clear();  
  6.     print(names);  
  7.   
  8. }  

Output:

Example - Remove All Element to the given Set
{Peter, John, Ricky, Devansh, Finch}
{}

TypeCast Set to List

The Set object can convert into the List Object using the toList() method. The syntax is as follows.

Note - The type of List must be the same as the type of Set.

Syntax -

  1. List<type> <list_name> = <set_name>. toList();  

Dart Set Operations

Dart Set provides the facility to perform following set operations. These operations are given below.

Union - The union is set to combine the value of the two given sets a and b.

Intersection - The intersection of the two set a and b returns all elements, which is common in both sets.

Subtracting - The subtracting of two sets a and b (a-b) is the element of set b is not present in the set a.

Let's understand the following example.

Example -

  1. void main()  {  
  2.   
  3.     var x = <int>{10,11,12,13,14,15};  
  4.     var y = <int>{12,18,29,43};  
  5.     var z = <int>{2,5,10,11,32};  
  6.     print("Example - Set Operations");  
  7.       
  8.     print("x union y is -");  
  9.     print(x.union(y));  
  10.   
  11.     print("x intersection y is - ");  
  12.     print(x.intersection(y));  
  13.       
  14.     print("y difference z is - ");  
  15.      print(y.difference(z));   
  16.       
  17. }  

Output:

Example - Set Operations
x union y is -
{10, 11, 12, 13, 14, 15, 18, 29, 43}
x intersection y is -
{12}
y difference z is -
{12, 18, 29, 43} 

Dart Set Properties

The few properties of the Dart set as follows.

PropertiesExplanations
firstIt is used to get the first element in the given set.
isEmptyIf the set does not contain any element, it returns true.
isNotEmptyIf the set contains at least one element, it returns true
lengthIt returns the length of the given set.
lastIt is used to get the last element in the given set.
hashcodeIt is used to get the hash code for the corresponding object.
SingleIt is used to check whether a set contains only one element.

Comments