Dart Programming - Collection Set

Set represents a collection of objects in which each object can occur only once. The dart:core library provides the Set class to implement the same.
Identifier = new Set()

OR
Identifier = new Set.from(Iterable)

Where, Iterable represents a list of values to add to a Set.
void main() { 
   Set numberSet = new  Set(); 
   numberSet.add(100); 
   numberSet.add(20); 
   numberSet.add(5); 
   numberSet.add(60); 
   numberSet.add(70);
   print("Default implementation :${numberSet.runtimeType}");  
   
   // all elements are retrieved in the order in which they are inserted 
   for(var no in numberSet) { 
      print(no); 
   } 
}    
It should produce the following output −
100 
20 
5 
60 
70 

void main() { 
   Set numberSet = new Set.from([12,13,14]); 
   print("Default implementation :${numberSet.runtimeType}");  
   // all elements are retrieved in the order in which they are inserted 
   for(var no in numberSet) { 
      print(no); 
   } 
}
It should produce the following output −
12 
13 
14

The dart:collection library provides classes that enable various implementations of Dart collections. We will discuss the following topics in this section.
HashMap
HashSet
LinkedList
Queue
A HashMap is a hash table based implementation of Map. When you iterate through a HashMap's keys or values, you cannot expect a certain order. The syntax for the same is as given below −
Identifier= new HashMap()

The following example shows how you can implement a HashMap −
import 'dart:collection'; 
main() { 
   var accounts = new HashMap(); 
   accounts['dept']='HR'; 
   accounts['name']='Tom'; 
   accounts['email']='tom@xyz.com'; 
   print('Map after adding  entries :${accounts}'); 
}
It should produce the following output −
Map after adding entries :{email: tom@xyz.com, dept: HR, name: Tom}

The HashMap class inherits the addAll() function from the Map class. This function enables adding multiple values all at once.
HashMap.addAll(Iterable)

Where, Iterable represents a list of values to be inserted.
import 'dart:collection'; 
main() { 
   var accounts = new HashMap(); 
   accounts.addAll({'dept':'HR','email':'tom@xyz.com'}); 
   print('Map after adding  entries :${accounts}'); 
}
It should produce the following output −
Map after adding  entries :{email: tom@xyz.com, dept: HR} 

The remove() and the clear() functions are used to remove entries from the HashMap. The remove() function is passed a key that represents the entry to be removed. The clear() function is used to remove all the entries from the Map.
import 'dart:collection'; 
main() { 
   var accounts = new HashMap(); 
   accounts['dept'] = 'HR'; 
   accounts['name'] = 'Tom'; 
   accounts['email'] = 'tom@xyz.com'; 
   print('Map after adding  entries :${accounts}');
   accounts.remove('dept'); 
   print('Map after removing  entry :${accounts}');  
   accounts.clear(); 
   print('Map after clearing entries :${accounts}'); 
} 
It should produce the following output −
Map after adding  entries :{email: tom@xyz.com, dept: HR, name: Tom} 
Map after removing  entry :{email: tom@xyz.com, name: Tom} 
Map after clearing entries :{}

A HashSet is an unordered hash-table based Set implementation. The syntax for the same is −
Identifier = new HashSet() 

The add() function can be used to populate a HashSet instance.
import 'dart:collection'; 
void main() { 
   Set numberSet = new  HashSet(); 
   numberSet.add(100); 
   numberSet.add(20); 
   numberSet.add(5); 
   numberSet.add(60); 
   numberSet.add(70); 
   print("Default implementation :${numberSet.runtimeType}"); 
   for(var no in numberSet){ 
      print(no); 
   }
}   
It should produce the following output −
60 
20 
100 
5 
70

The addAll() function allows adding multiple values to the HashSet. The following example illustrates the same −
import 'dart:collection'; 
void main() { 
   Set numberSet = new  HashSet(); 
   numberSet.addAll([100,200,300]); 
   print("Default implementation :${numberSet.runtimeType}"); 
   for(var no in numberSet){ 
      print(no); 
   } 
}
It should produce the following output −
Default implementation :_HashSet 
200 
300 
100 

The remove() function removes the value passed to it. The clear() function removes all the entries from the HashSet.
import 'dart:collection'; 
void main() { 
   Set numberSet = new  HashSet(); 
   numberSet.addAll([100,200,300]); 
   print("Printing hashet.. ${numberSet}");  
   numberSet.remove(100); 
   print("Printing hashet.. ${numberSet}");  
   numberSet.clear(); 
   print("Printing hashet.. ${numberSet}"); 
} 
It should produce the following output −
Printing hashet.. {200, 300, 100} 
Printing hashet.. {200, 300} 
Printing hashet.. {}


    44 Lectures 
     4.5 hours 

 Sriyank Siddhartha

    34 Lectures 
     4 hours 

 Sriyank Siddhartha

    69 Lectures 
     4 hours 

 Frahaan Hussain

    117 Lectures 
     10 hours 

 Frahaan Hussain

    22 Lectures 
     1.5 hours 

 Pranjal Srivastava

    34 Lectures 
     3 hours 

 Pranjal Srivastava
 Print
 Add Notes
 Bookmark this page