Table of contents
1.
Introduction
2.
Collections
2.1.
List
2.2.
Set
2.3.
Map
2.4.
Queue
3.
Frequently Asked Questions
3.1.
Does a list only contain elements of the same data types?
3.2.
Can we have the same values corresponding to different keys in a map?
3.3.
How can you remove an element from a map?
4.
Conclusion 
Last Updated: Mar 27, 2024

Dart Collections

Author Pradeep Kumar
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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

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

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}

Frequently Asked Questions

Does a list only contain elements of the same data types?

No, a list can contain elements of different data types also.

 

Can we have the same values corresponding to different keys in a map?

Keys in a map are unique. But there can be the same values corresponding to more than one key.

 

How can you remove an element from a map?

Elements can be deleted from a map using the remove function. To do so, simply pass the key of that element to the remove function.

Conclusion 

In this article, we have extensively discussed the Collections in Dart. We hope that this blog has helped you enhance your knowledge regarding the Dart Collection, and to learn more, check out our other article on Dart Collection Methods. And also check out the awesome content on the Coding Ninjas Website,
Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test SeriesDo upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass