In this guide, we will discuss collection set in Dart programming language . 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.
Syntax
Identifier = new Set()
OR
Identifier = new Set.from(Iterable)
Where, Iterable represents a list of values to add to a Set.
Example
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
Illustration: Set.from()
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
Advanced Dart Collection ─ dart: collection Library
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
HashMap
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 −
Syntax
Identifier= new HashMap()
Example
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}
Adding Multiple Values to a HashMap
The HashMap class inherits the addAll() function from the Map class. This function enables adding multiple values all at once.
Syntax
HashMap.addAll(Iterable)
Where, Iterable represents a list of values to be inserted.
Example
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}
Removing Values from a HashMap
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.
Example
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 :{}
HashSet
A HashSet is an unordered hash-table based Set implementation. The syntax for the same is −
Syntax
Identifier = new HashSet()
The add() function can be used to populate a HashSet instance.
Example
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
Adding Multiple Values to a HashSet
The addAll() function allows adding multiple values to the HashSet. The following example illustrates the same −
Example
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
Removing Values from a HashSet
The remove() function removes the value passed to it. The clear() function removes all the entries from the HashSet.
Example
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.. {}
Next Topic : Click Here
Pingback: Dart Programming - Collection List | Adglob Infosystem Pvt Ltd
Pingback: Dart Programming - Collection | Adglob Infosystem Pvt Ltd