Introduction to collection in Dart
Collection is a group of similar type of objects. Dart supports the following types of collections:
- List
- Set
- Map
List
List is an ordered group of objects. Dart supports both growable and fixed length list. List is used to store the multiple values in a single variable. List is a zero based index based collection. Lists in dart are similar to arrays in other languages. List can be created using the following ways:
- Using List literal
- Using List constructor
Using List literal
List literal is a collection of objects surrounded by square brackets. List literal is a growable list.
void main() {
var list = [1,2,3,4,5];
print(list);
}
Output
[1,2,3,4,5]
In the above example, we have created a list of integers which increases the size or length of the list as the values increases. We will deep dive on this topic in the up coming lessons.
Using List constructor
List constructor is used to create a fixed length list.
void main() {
var list = new List(3);
list[0] = 12;
list[1] = 13;
list[2] = 11;
print(list);
}
Output
[12,13,11]
In the above example, we have created a list of integers which is fixed in size. We will deep dive on this topic in the up coming lessons.
Set
Set is an unordered group of unique objects. Dart supports both growable and fixed length set. Set is used to store the multiple values in a single variable. Set is a zero based index based collection. Set in dart is similar to set in mathematics. Set can be created using the following ways:
- Using Set literal
- Using Set constructor
Using Set literal
Set literal is a collection of objects surrounded by curly braces. Set literal is a growable set.
void main() {
var set = {1,2,3,4,5};
print(set);
}
Output
{1,2,3,4,5}
In the above example, we have created a set of integers which increases the size or length of the set as the values increases. We will deep dive on this topic in the up coming lessons.
Using Set constructor
Set constructor is used to create a fixed length set.
void main() {
var set = new Set.from([12,13,11]);
print(set);
}
Output
{12,13,11}
In the above example, we have created a set of integers which is fixed in size. We will deep dive on this topic in the up coming lessons.
Map
Map is an unordered group of key-value pair of objects. Dart supports both growable and fixed length map. Map is used to store the multiple values in a single variable. Map is a zero based index based collection. Map in dart is similar to dictionary in other languages. Map can be created using the following ways:
- Using Map literal
- Using Map constructor
Using Map literal
Map literal is a collection of key-value pair of objects surrounded by curly braces. Map literal is a growable map.
void main() {
var map = {1:'Tom',2:'John',3:'Hary'};
print(map);
}
Output
{1: 'Tom', 2: 'John', 3: 'Hary }
In the above example, we have created a map of integers which increases the size or length of the map as the values increases. We will deep dive on this topic in the up coming lessons.
Using Map constructor
Map constructor is used to create a fixed length map.
void main() {
var map = new Map();
map[1] = 'Tom';
map[2] = 'John';
map[3] = 'Hary';
print(map);
}
Output
{ 1: Tom, 2: John, 3: Hary}
In the above example, we have created a map of integers which is fixed in size. We will deep dive on this topic in the up coming lessons.
Conclusion
In this lesson, we have covered the following topics:
- Introduction to collection in Dart
- List
- Set
- Map