Introduction
In this blog, we will look into the Dart Collections. In Dart, collections are used to aggregate together similar objects into a single unit. Collections allow us to store, retrieve, manipulate, and aggregate data.
The second part of this article is on Dart Collection Methods, do check it out after reading this. If you are new to Dart and are yet to set up your environment for the language, you can check out this article.
Collections
A collection contains a number of objects, generally called elements or items. Some of the most commonly used collections are:
- List
- Set
- Map
- Queue
List
The Dart list is an ordered collection in which any entry may be accessed by its index. Elements in a list can be repeated an unlimited number of times.
In the example below, we will see how a list can be initialized and how we can print the elements of it:
// Main function to run the program
void main() {
// Initialising a list
List l = [];
// Adding elements in the list
l.add(1);
l.add(8);
l.add(27);
l.add(64);
l.add(125);
// Printing elements of the list
for (int i = 0; i < l.length; i++) {
print("The element is: " + l[i].toString());
}
// Printing the whole list
print("The list is: " + l.toString());
}
Output:
The element is: 1
The element is: 8
The element is: 27
The element is: 64
The element is: 125
The list is: [1, 8, 27, 64, 125]
Set
A set is a grouping of elements that are not in any particular sequence. It does not allow duplicate elements, so every element in a set is distinct. The arrangement of the set items has little effect in most circumstances.
We'll see how to declare a set in Dart by looking at the example below:
// Main function to run the program
void main() {
// Initialising a set
Set s = {};
// Adding elements to the set
s.add("Red");
s.add("Blue");
s.add("Green");
s.add("Red"); // Adding Red again to the set
s.add("Black");
// Printing the elements of set
for (var ele in s) {
print("The element is: " + ele);
}
// Printing the whole set
print("The set is: " + s.toString());
}
Output:
The element is: Red
The element is: Blue
The element is: Green
The element is: Black
The set is: {Red, Blue, Green, Black}
As you can see from the above example, even though we added the element "Red" twice in the set, there is still only one copy of that element present in the set.
Map
Elements of a map are stored as key-value pairs. Each key maps exactly to one value. Keys are unique. For example,
// Main function to run the program
void main() {
// Initialising a map
Map<int, int> mp = Map();
// Adding elements to the set
mp[1] = 1;
mp[2] = 4;
mp[3] = 9;
mp[4] = 16;
mp[5] = 25;
// Printing the keys of map
print("The keys of the map are: " + mp.keys.toString());
// Printing the values of map
print("The values of the map are: " + mp.values.toString());
// Printing the whole map
print("The map is: " + mp.toString());
}
Output:
The keys of the map are: (1, 2, 3, 4, 5)
The values of the map are: (1, 4, 9, 16, 25)
The map is: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Queue
A queue is a common data structure for inserting data in a first-in-first-out (FIFO) fashion. Dart allows you to add elements to a queue from both sides. For example,
import 'dart:collection';
// Main function to run the program
void main() {
// Initialising a queue
Queue q = new Queue();
// Adding elements to the queue
// Adding elements from the front
q.addFirst("Mumbai");
q.addFirst("Delhi");
// Adding elements from the back
q.addLast("Jaipur");
q.addLast("Gurgaon");
// Adding elements from the front
q.addFirst("Hyderabad");
q.addFirst("Chennai");
// Printing the elements of a queue
for (var ele in q) {
print("The element is: " + ele);
}
// Printing the whole map
print("The queue is: " + q.toString());
}
Output:
The element is: Chennai
The element is: Hyderabad
The element is: Delhi
The element is: Mumbai
The element is: Jaipur
The element is: Gurgaon
The queue is: {Chennai, Hyderabad, Delhi, Mumbai, Jaipur, Gurgaon}




