Dart Sets
Sets data type in Dart
A set is an unordered collection of items that are unique. Dart infers that sets tale strings as values.
Example,
var languages = { ‘Dart’ , ‘Flutter’, ‘Python’, ‘Java’ };
There are some salient functions that can be used along with sets,
- add( ) or addAll( ) methods are the functions used to add elements in the existing set.
- .length is used to extract the number of items in the set.
Program:
void main( )
{
// declares and defines set languages
var languages = { 'Dart' , 'Flutter' , 'Python' , 'Java' };
// prints the values of set
print( languages );
// creates an empty string
var lang = < String > { };
print( lang );
// add( ) to add the value in set languages
languages.add( 'php' );
print( languages );
}
Output :
{Dart, Flutter, Python, Java}
{}
{Dart, Flutter, Python, Java, php}
Set Operations in Dart
- Union = This operation merges two strings and returns the merged string.
- Intersection = This operation returns the common strings out of the two strings.
- Difference ( string A, string B ) = This operation returns the string present in string A but not in B.
Consider the following code in Dart that illustrates the above operations:
Program
// Set Operations in Dart
void main( )
{
// declaring set 1 with string values
var set1 = < String > { "India", "will", "win", "T-20" };
// printing the values of set 1
print( " Set 1 is as follows : " );
print( set1 );
// declaring set 2 with string values
var set2 = < String > { "World", "Cup", "this", "year." };
// declaring set 3 with string values
var set3 = < String > { "World", "India", "win", "year." };
// printing the values of set 2
print( "\n\n Set 2 is as follows : " );
print( set2 );
// Performing union on set1 and set2
print( "\n Union of two sets is ${ set1.union( set2 ) } \n " ) ;
// Performing intersection on set1 and set2
print( " Intersection of sets 1 and 2 is ${ set1.intersection( set2 ) } \n ");
// Performing intersection on set2 and set3
print( " Intersection of sets 2 and 3 is ${ set2.intersection( set3 ) } \n " );
// Performing difference ( Set2 - Set1 )
print( " Difference of sets 2 and 1 is ${ set2.difference( set1 ) } \n " );
// Performing difference ( Set1 - Set2 )
print( " Difference of sets 1 and 2 is ${ set1.difference( set2 ) } \n " );
// Performing difference ( Set2 - Set3 )
print( " Difference of sets 2 and 3 is ${ set2.difference( set3 ) } \n " );
}
Output
Set 1 is as follows :
{India, will, win, T-20}
Set 2 is as follows :
{World, Cup, this, year.}
Union of two sets is {India, will, win, T-20, World, Cup, this, year.}
Intersection of sets 1 and 2 is {}
Intersection of sets 2 and 3 is {World, year.}
Difference of sets 2 and 1 is {World, Cup, this, year.}
Difference of sets 1 and 2 is {India, will, win, T-20}
Difference of sets 2 and 3 is {Cup, this}
Sets Functions
S. No. | Syntax | Description |
1. | variable_name.elementAt( index ); | Returns the element present at the particular index, passed as an argument. |
2. | variable_name.length; | Calculates the length of the set, and returns the same. |
3. | variable_name.contains( element_name ); | Checks if the element_name is present in the set or not, and returns true or false accordingly. |
4. | variable_name.remove( element_name ); | Deletes the element that is passed as an argument to the function. |
5. | variable_name.forEach(…); | Implements the command defined inside forEach( ) function for all the elements inside the set. |
6. | variable_name.clear( ); | Deletes all the element inside the set. |