Types of Dart Collections
Dart collections are a set of classes that provide data structures to store and manipulate groups of objects. Dart's core library provides several types of collections, including lists, sets, and maps. These collections are essential for managing and processing data efficiently in Dart programs.
Types of Dart Collections
List:
An ordered collection of objects.
Each element in a list is indexed, starting from 0.
Dart provides
List
and its various implementations, such asList
(growable list) andFixedLengthList
.
// Example of a List
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
print(numbers[0]); // Output: 1
numbers.add(6);
print(numbers); // Output: [1, 2, 3, 4, 5, 6]
}
Set:
An unordered collection of unique items.
It does not allow duplicate elements.
Useful for operations that require uniqueness and set operations like union and intersection.
// Example of a Set
void main() {
Set<String> fruits = {'apple', 'banana', 'orange'};
fruits.add('apple'); // Duplicate element, will not be added
print(fruits); // Output: {apple, banana, orange}
}
Map:
A collection of key-value pairs, where each key is unique.
Keys and values can be of any type.
Provides fast lookup based on keys.
// Example of a Map
void main() {
Map<String, int> ages = {
'Alice': 25,
'Bob': 30,
'Charlie': 35
};
print(ages['Alice']); // Output: 25
ages['Dave'] = 40;
print(ages); // Output: {Alice: 25, Bob: 30, Charlie: 35, Dave: 40}
}
Common Collection Operations
Iteration:
void main() { List<int> numbers = [1, 2, 3, 4, 5]; for (int number in numbers) { print(number); // Output: 1 2 3 4 5 } }
Filtering:
void main() { List<int> numbers = [1, 2, 3, 4, 5]; var evenNumbers = numbers.where((number) => number.isEven); print(evenNumbers.toList()); // Output: [2, 4] }
Mapping:
void main() { List<int> numbers = [1, 2, 3, 4, 5]; var squares = numbers.map((number) => number * number); print(squares.toList()); // Output: [1, 4, 9, 16, 25] }
Reducing:
void main() { List<int> numbers = [1, 2, 3, 4, 5]; var sum = numbers.reduce((a, b) => a + b); print(sum); // Output: 15 }
Specialized Collections
Dart also provides specialized collections through the collection
package, which includes more advanced data structures like:
Queue:
An implementation of a double-ended queue.
Supports adding and removing items from both ends.
import 'dart:collection';
void main() {
Queue<int> queue = Queue();
queue.addAll([1, 2, 3]);
queue.addFirst(0);
queue.addLast(4);
print(queue); // Output: {0, 1, 2, 3, 4}
}
LinkedHashMap:
- A hash map that maintains the insertion order of keys.
import 'dart:collection';
void main() {
LinkedHashMap<String, int> map = LinkedHashMap();
map['Alice'] = 25;
map['Bob'] = 30;
map['Charlie'] = 35;
print(map); // Output: {Alice: 25, Bob: 30, Charlie: 35}
}
Conclusion
Dart collections are essential tools for managing data in your Dart applications. They offer a variety of data structures that cater to different needs, from ordered lists to unique sets and key-value maps. Understanding and using these collections effectively can significantly enhance the performance and readability of your code.
Subscribe to my newsletter
Read articles from Michael Piper directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Michael Piper
Michael Piper
Experienced Software Engineer skilled in creating mobile apps and web solutions. Expertise in iOS/Android app development, JavaScript frameworks, Python, and research methodologies. Detail-oriented problem solver with 10+ years of experience delivering top-notch solutions.