×

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,

  1. add( ) or addAll( ) methods are the functions used to add elements in the existing set.
  2. .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

  1. Union = This operation merges two strings and returns the merged string.
  2. Intersection = This operation returns the common strings out of the two strings.
  3. 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.SyntaxDescription
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.

Related Topics

Dart If-else statement

In the previous section, we studied about if statements in detail. If – block is executed when the condition evaluates to true, else if the condition evaluates to false, then...

1 minute read.

Dart Generators

Synchronous Generator Dart Generator is a unique function that allows us to generate price sequences. Generators return values when needed; means that the value is generated if we try to duplicate...

5 minutes read.

Dart Isolates

Dart successfully supports asynchronous programming which runs our program without any hindrance. This asynchronous programming is used to achieve concurrency. Dart supports concurrent programming with features such as ‘ async-await...

9 minutes read.

Dart Exception Handling

Exception Class – An exception is any runtime error that leads to abrupt termination of the program. It helps in addressing the error programmatically. It is aimed to be caught...

4 minutes read.

Dart Types of Functions

Functions are the set of statements intended to perform a specific task. They accept some input from the user, perform the specified computation and generate the output. They are very...

4 minutes read.

Dart Maps

Map is an object-based data structure that associates keys and values that can be of any data type. Every key can occur only once, while the values can be used multiple...

5 minutes read.

Dart Method Overriding

Before understanding method overriding, it is important to clear the concept of polymorphism. Polymorphism is derived from two Greek words 'Poly' which means many and 'morphs' which means many forms....

5 minutes read.

Dart Loops

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

4 minutes read.

Dart Strings

Strings data type in Dart A Dart string is a sequence of characters with an encoding of UTF-16. The text associated with string data type is enclosed withing single (‘  ’)...

3 minutes read.

Dart Syntax

In the previous tutorial, we coded our first program in Dart on various platforms understanding the basic functionality of each line of code.  Let us understand the syntax for Dart language...

4 minutes read.

Unit Testing in Dart

Typing testing ensures that your app keeps working as you add more features or change existing functionality. Unit testing helps to confirm the behavior of a single function, method, or...

4 minutes read.

Dart Switch Case Statement

In the case of if-else statements, we prefer not to use the if-else ladder when there are many test conditions to be evaluated. In such a situation, it is preferable...

4 minutes read.

Builder Classes in Dart

Before understanding builder classes, let us understand about Flutter that makes the best use of the builder classes. Flutter is actually not a programming language, it is a software development...

6 minutes read.

Dart super keyword

The ' super ' keyword is used to refer to the immediate parent class object of the currently in-use child class. Using this keyword, we can invoke the superclass (...

6 minutes read.

Dart Miscellaneous Operators

Apart from the operators we studied so far, Dart provides some more operators which are as follows : Conditional OperatorCascade Notations Conditional Operators These are the operators that help in evaluating the expression...

1 minute read.

Dart Built-in Data Types

A data type defines the type of value a variable can hold, such as integer, double, or string.  Dart supports two data types:  1. Built-in data types : These are the data...

1 minute read.

Dart Queues

A queue is a user-defined collection of data. It is based on the FIFO principle ( First In First Out ) which implies that the element to be inserted first,...

3 minutes read.

Dart Comments

Comments are the statements that are used to enhance the readability and understandability of the code. The compiler does not execute these lines. It simply ignores these comments when it scans...

2 minutes read.

Dart Inheritance

Inheritance is a promising concept of any programming language. It is a property by which a class inherits the property of another class. Moreover, the class deriving the properties of...

5 minutes read.

Dart Standard Input & Output

Standard Input in Dart (stdin): The standard input stream reads data both synchronously and asynchronously from the keyboard.  In Dart programming language, .readLineSync( ) function is used to accept input from the...

3 minutes read.