Introduction
In this blog, we will look into the Dart Generics. All the collections in Dart are, by default, of heterogeneous nature. We will discuss how we can use generics to construct collections that can hold homogeneous values. Before moving forward with this blog, I suggest you check out our other blog on Dart Collections, which will help you understand the concept of Generics better.
Dart Generics
Collections in Dart are heterogeneous by default, i.e., a single collection can hold values of different data types. A Dart collection, on the other hand, can be made to store values that are all the same. Generics can be used to accomplish the same goal.
When Generics are used, the data type of the values held within the collection is restricted. Type-safe collections are just that. A type-safe collection is declared with a pair of angle brackets enclosing the data type. The following is the syntax for the same:
Syntax:
CollectionName <dataType> identifier= new CollectionName<dataType>
In the further sections, we will see how type safety can be accomplished in collections like List, Map, Set, and Queue.
Generic List
A List is essentially an ordered group of objects. In the example below, we will see how a list of integer data type can be created.
void main() {
// Creating a list of integers
List<int> squares = [];
// Adding elements in the list
squares.add(1);
squares.add(4);
squares.add(9);
squares.add(16);
squares.add(25);
// Printing the list
print(squares);
}
Output:
[1, 4, 9, 16, 25]
Generic Map
A map consists of a set of key-value pairs. Every key has a corresponding value associated with it. The datatype of key and value doesn't have to be the same. For example,
void main() {
// Creating a set of integers
Map<int, String> mp = {};
// Adding elements in the map
mp[1] = "Red";
mp[2] = "Blue";
mp[3] = "Black";
// Printing the Map
print(mp);
}
Output:
{1: Red, 2: Blue, 3: Black}
Here, the map mp is a mapping from integer to Strings. It holds integer keys and colors corresponding to those keys.
Generic Set
A set is identical to a list, except that it only contains unique values. In case you add one element multiple times; only one value of the same will be stored in the set. For example,
void main() {
// Creating a set of integers
Set<int> st = {};
// Adding elements in the Set
st.add(1);
st.add(2);
st.add(3);
st.add(1);
// Printing the Set
print(st);
}
Output:
{1, 2, 3}
Here, we have added element "1" twice in the set. But, as you can see from the output, the final set contains only one value of that element.
Generic Queue
A queue is a popular data structure used to insert data in FIFO(First In First Out) manner. Dart provides the functionality of adding elements from both ends in a queue. For example,
import 'dart:collection';
// Main function to run the program
void main() {
Queue<String> colours = new Queue<String>();
// Inserting elements from the front
colours.addFirst("Red");
colours.addFirst("Blue");
// Inserting elements from the back
colours.addLast("Green");
colours.addLast("Black");
// Inserting elements from the front
colours.addFirst("Orange");
colours.addFirst("Yellow");
// Printing the queue
print(colours);
}
Output:
{Yellow, Orange, Blue, Red, Green, Black}