×

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 this Keyword

The ' this ' keyword, similar to that in Java and C#, refers to an object of the class using it. It points to the current object of the class,...

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.

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.

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 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’,...

3 minutes read.

Dart Logical Operators

Logical operators are the operators that combine two or more conditions, and accordingly return a Boolean value : true or false. Assume the value of variable A is 25 and B...

2 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.

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.

Assert in DART

In any programming language, resolving error is the most unwanted and tedious task. At times it becomes very difficult to find the error in the large code. Dart offers an...

3 minutes 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.

Soundness in Dart

What is soundness? Soundness ensures that the program never comes across any invalid state that may lead to abrupt termination of the program. As its name suggests it ensures perfect type...

4 minutes read.

Dart If-else-if statement

If - else - if statement enables to check the set of test expressions that evaluate to Boolean True or False, and based on this true or false the particular...

2 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.

Typedef in Dart

In Dart, typedef is used to generate a function type that we can use as a type annotation for declaring variables and return types of the function type. An alias...

4 minutes read.

Dart Equality and Relational Operators

Dart provides the functionality of checking the relationship between the values or values within the variables. These operators return the resultant value as Boolean, i.e., either ‘true’ or ‘false’. Some important points...

3 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.

Type System in Dart

Dart language is a type safety enabled language that uses static and dynamic type checking to match the value of the variable with its data type at the compile-time. This...

4 minutes read.

Dart HTML DOM

All web pages can be viewed as objects and exist within the browser window. We can access a web page using a web browser and it needs to be connected...

3 minutes read.

Metadata in Dart

Metadata is often referred to as the data about the data. It is a  piece of data about a basic piece of data. In the case of a Dart program,...

2 minutes read.

Dart Concurrency

Dart concurrency allows us to run multiple or multiple components of a system at once. It issues several commands at once. Dart provides Isolates as a tool for performing compliance...

2 minutes read.