Collections are groups of objects that represent a particular element. The dart::collection library is used to implement the collection in dart. There are a variety of collections available in dart.
Some of the classes of Dart Collection are –
- List<E>: A List is an ordered group of objects.
- Set<E>: Collection of objects in which each of the objects occurs only once
- Map<K, V>: Collection of objects that has a simple key/value pair-based object. The key and value of a map can be of any type.
- Queue<E>: A Queue is a collection that can be manipulated at both ends. One can iterate over a queue with an Iterator or using forEach.
- DoubleLinkedQueue<E>: Doubly-linked list based on the queue data structure.
- HashMap<K, V>: Map based on hash table i.e unordered map.
- HashSet<E>: Set based on hash table i.e unordered set.
- LinkedHashMap<K, V>: Similar to HashMap but based on LinkedList.
- LinkedHashSet<E>: Similar to HashSet but based on LinkedList.
- LinkedList<E extends LinkedListEntry<E>>: It is a specialized double-linked list of elements.
- LinkedListEntry<E extends LinkedListEntry<E>>: An element of a LinkedList.
- MapBase<K, V>: This is the base class for Map.
- UnmodifiableListView<E>: An unmodifiable List view of another List.
- UnmodifiableMapBase<K, V>: Basic implementation of an unmodifiable Map.
- UnmodifiableMapView<K, V>: Unmodifiable view of the map.
We will be discussing the 4 basic collections with examples here.
1. List<E>
The list is an ordered group of objects where each object is from one specific type. To define a list in dart, specify the object type inside the angled brackets (<>) as shown below:
List<String> fruits = ["Mango", "Apple", "Banana"]
Example:
Here we have defined a list and performed some common operations along with some basic commonly used methods.
Dart
void main() {
List geekList = new List();
geekList.addAll([1,2,3,4,5, "Apple" ]);
print(geekList);
for (var i = 0; i<geekList.length;i++){
print( "element $i is ${geekList[i]}" );
}
geekList.removeAt(2);
geekList. remove ( "Apple" );
print(geekList);
print(geekList.reversed);
print(geekList.isEmpty);
print(geekList.first);
geekList = Iterable< int >.generate(10).toList();
print(geekList);
}
|
Output:
[1, 2, 3, 4, 5, Apple]
element 0 is 1
element 1 is 2
element 2 is 3
element 3 is 4
element 4 is 5
element 5 is Apple
[1, 2, 4, 5]
(5, 4, 2, 1)
false
1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2. Set<E>
Sets are one of the essential part of Dart Collections. A set is defined as an unordered collection of unique objects. To define a set follow the below:
Set fruits = Set.from("Mango", "Apple", "Banana")
Example:
As discussed earlier a set stores a group of objects that are not repeating. A sample program is shown below.
Dart
void main() {
Set geekSet = new Set();
geekSet.addAll([9,1,2,3,4,5,6,1,1,9]);
for (var el in geekSet){
print(el);
}
print( 'Length: ${geekSet.length}' );
print( 'First Element: ${geekSet.first}' );
geekSet. remove (10);
geekSet. remove (9);
print(geekSet);
}
|
Output:
9
1
2
3
4
5
6
Length: 7
First Element: 9
{1, 2, 3, 4, 5, 6}
3. Map<K, V>
In Dart, Maps are unordered key-value pair collection that sets an associate key to the values within. To define a Map, specify the key type and the value type inside the angle brackets(<>) as shown below:
Map<int, string> fruits = {1: "Mango", 2:"Apple", 3:"Banana"}
Example:
The map collection stores the objects as a key-value pair. An example is shown below.
Dart
void main() {
var geekMap = {1: "Apple" ,2: "Mango" ,3: "Banana" };
print(geekMap);
geekMap.addAll({4: 'Pineapple' ,2: 'Grapes' });
geekMap[9]= "Kiwi" ;
print(geekMap);
print( 'Keys: ${geekMap.keys} \nValues: ${geekMap.values}' );
geekMap. remove (2);
print( '{$geekMap} length is ${geekMap.length}' );
}
|
Output:
{1: Apple, 2: Mango, 3: Banana}
{1: Apple, 2: Grapes, 3: Banana, 4: Pineapple, 9: Kiwi}
Keys: (1, 2, 3, 4, 9)
Values: (Apple, Grapes, Banana, Pineapple, Kiwi)
{{1: Apple, 3: Banana, 4: Pineapple, 9: Kiwi}} length is 4
4. Queue<E>
Queues are used to implement FIFO(First in First Out) collection. This collection can be manipulated from both ends. A queue in dart is defined as follows:
Queue<String> queue = new Queue("Mango", "Apple","Banana")
Example:
Dart
import 'dart:collection' ;
void main() {
var geekQueue = new Queue();
geekQueue.addAll([9,1,2,3,4,5,6,1,1,9]);
geekQueue.addFirst( "GFG" );
geekQueue.addLast( "GFG2" );
print(geekQueue);
geekQueue.removeFirst();
print(geekQueue);
geekQueue.removeLast();
print(geekQueue);
print( 'First Element: ${geekQueue.first}' );
for (var el in geekQueue){
print(el);
}
print( 'Length: ${geekQueue.length}' );
geekQueue. remove (10);
geekQueue. remove (2);
print(geekQueue);
}
|
Output:
{GFG, 9, 1, 2, 3, 4, 5, 6, 1, 1, 9, GFG2}
{9, 1, 2, 3, 4, 5, 6, 1, 1, 9, GFG2}
{9, 1, 2, 3, 4, 5, 6, 1, 1, 9}
First Element: 9
9
1
2
3
4
5
6
1
1
9
Length: 10
{9, 1, 3, 4, 5, 6, 1, 1, 9}
Documentation
5. Hashmap
In the Dart programming language, a HashMap is a collection that stores key-value pairs, where the keys are unique and the values can be of any type. Here is an example of how to use a HashMap in Dart:
Dart
import 'dart:collection' ;
void main() {
var map = HashMap< int , String>();
map[1] = 'one' ;
map[2] = 'two' ;
map[3] = 'three' ;
print(map[1]);
map.forEach((key, value) {
print( '$key: $value' );
});
}
|
output:
one
two
three
6 : Hashset:
In the Dart programming language, a HashSet is a collection that stores a set of unique elements, where each element can be of any type. Here is an example of how to use a HashSet in Dart:
Dart
import 'dart:collection' ;
void main() {
var set = HashSet<String>();
set.add( 'apple' );
set.add( 'banana' );
set.add( 'cherry' );
print(set.contains( 'apple' ));
print(set.contains( 'pear' ));
set.forEach((element) {
print(element);
});
}
|
Output:
apple
banana
cherry
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
30 Dec, 2022
Like Article
Save Article