Sets in Dart is a special case in List where all the inputs are unique i.e it doesn’t contain any repeated input. It can also be interpreted as an unordered array with unique inputs. The set comes in play when we want to store unique values in a single variable without considering the order of the inputs. The sets are declared by the use of a set keyword.
There are two ways to do so:
var variable_name = <variable_type>{};
or,
Set <variable_type> variable_name = {};
Example 1: Declaring set in two different ways.
Dart
void main()
{
var gfg1 = <String>{ 'GeeksForGeeks' };
print( "Output of first set: $gfg1" );
Set<String> gfg2 = { 'GeeksForGeeks' };
print( "Output of second set: $gfg2" );
}
|
Output:
Output of first set: {GeeksForGeeks}
Output of second set: {GeeksForGeeks}
Example 2: Declaring repeated value in a set and a list and then comparing it.
Dart
void main()
{
var gfg = [ 'Geeks' , 'For' , 'Geeks' ];
print( "Output of the list is: $gfg" );
var gfg1 = <String>{ 'Geeks' , 'For' , 'Geeks' };
print( "Output of the set is: $gfg1" );
}
|
Output:
Output of the list is: [Geeks, For, Geeks]
Output of the set is: {Geeks, For}
Note: You can see that repeated value was simply ignored in the case of the set.
Adding Element In Set: To add an element in the set we make use of “.add()” function or “.addAll()” function. But you must note that if you try to add a duplicate value using these functions then too they will get ignored in a set.
// To add single value
variable_name.add(value);
// To add multiple value
variable_name.addAll(value1, value2, value3, ...valueN)the
Some other functions involving Sets are as follows:
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. |
Example:
Dart
void main()
{
var gfg = <String>{ 'Hello Geek' };
print( "Value in the set is: $gfg" );
gfg.add( "GeeksForGeeks" );
print( "Values in the set is: $gfg" );
var geeks_name = { "Geek1" , "Geek2" , "Geek3" };
gfg.addAll(geeks_name);
print( "Values in the set is: $gfg" );
var geek = gfg.elementAt(0);
print( "Element at index 0 is: $geek" );
int l = gfg.length;
print( "Length of the set is: $l" );
bool check = gfg.contains( "GeeksForGeeks" );
print( "The value of check is: $check" );
gfg. remove ( "Hello Geek" );
print( "Values in the set is: $gfg" );
print( " " );
print( "Using forEach in set:" );
gfg.forEach((element) {
if (element == "Geek1" )
{
print( "Found" );
}
else
{
print( "Not Found" );
}
});
gfg.clear();
print( "Values in the set is: $gfg" );
}
|
Output:
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: {}
Converting Set to List in Dart: Dart also provides us with the method to convert the set into the list. To do, so we make use of toList() method in Dart.
List<type> list_variable_name = set_variable_name.toList();
Note: It is useful in the way as the list we will get will contain unique values and no repeated values.
Example:
Dart
void main()
{
var gfg = <String>{ 'Hello Geek' , "GeeksForGeeks" , "Geek1" , "Geek2" , "Geek3" };
print( "Values in set are:" );
print(gfg);
print( "" );
List<String> gfg_list = gfg.toList();
print( "Values in the list are:" );
print(gfg_list);
}
|
Output:
Values in set are:
{Hello Geek, GeeksForGeeks, Geek1, Geek2, Geek3}
Values in the list are:
[Hello Geek, GeeksForGeeks, Geek1, Geek2, Geek3]
Converting Set to Map in Dart: Dart also provides us with the method to convert the set into the map.
Example:
Dart
void main()
{
var gfg = <String>{ "GeeksForGeeks" , "Geek1" , "Geek2" , "Geek3" };
var geeksforgeeks = gfg.map((value) {
return 'mapped $value' ;
});
print( "Values in the map:" );
print(geeksforgeeks);
}
|
Output:
Values in the map:
(mapped GeeksForGeeks, mapped Geek1, mapped Geek2, mapped Geek3)
Set Operations in Dart: There are three operations on set in Dart:
Example:
Dart
void main()
{
var gfg1 = <String>{ "GeeksForGeeks" , "Geek1" , "Geek2" , "Geek3" };
print( "Values in set 1 are:" );
print(gfg1);
print( "" );
var gfg2 = <String>{ "GeeksForGeeks" , "Geek3" , "Geek4" , "Geek5" };
print( "Values in set 2 are:" );
print(gfg2);
print( "" );
print( "Union of two sets is ${gfg1.union(gfg2)} \n" );
print( "Intersection of two sets is ${gfg1.intersection(gfg2)} \n" );
print( "Difference of two sets is ${gfg2.difference(gfg1)} \n" );
}
|
Output:
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}
In the above code, we can also compare more than two sets as:
Example:
Dart
void main()
{
var gfg1 = <String>{ "GeeksForGeeks" , "Geek1" , "Geek2" , "Geek3" };
var gfg2 = <String>{ "GeeksForGeeks" , "Geek3" , "Geek4" , "Geek5" };
var gfg3 = <String>{ "GeeksForGeeks" , "Geek5" , "Geek6" , "Geek7" };
print( "Union of two sets is ${gfg1.union(gfg2).union(gfg3)}\n" );
print( "Intersection of two sets is ${gfg1.intersection(gfg2).intersection(gfg3)}\n" );
print( "Difference of two sets is ${gfg2.difference(gfg1).difference(gfg3)}\n" );
}
|
Output:
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}