Files

160 lines
3.6 KiB
Markdown
Raw Permalink Normal View History

# Dart Collections: A Comprehensive Guide
Dart provides several built-in collection types to help you manage and manipulate groups of objects. This guide covers the main collection types and their operations.
## 1. Lists
Lists are ordered, indexable collections of objects.
### Example:
```dart
void main() {
// Creating lists
var numbers = [1, 2, 3, 4, 5];
List<String> fruits = ['apple', 'banana', 'orange'];
// Accessing elements
print(numbers[0]); // 1
print(fruits.last); // orange
// Adding elements
numbers.add(6);
fruits.addAll(['grape', 'mango']);
// Removing elements
numbers.remove(3);
fruits.removeAt(0);
// Other operations
print(numbers.length); // 5
print(fruits.contains('banana')); // true
// Iterating
for (var fruit in fruits) {
print(fruit);
}
// Functional operations
var doubledNumbers = numbers.map((n) => n * 2).toList();
var evenNumbers = numbers.where((n) => n.isEven).toList();
}
```
## 2. Sets
Sets are unordered collections of unique items.
### Example:
```dart
void main() {
// Creating sets
var uniqueNumbers = {1, 2, 3, 4, 5};
Set<String> uniqueFruits = {'apple', 'banana', 'orange'};
// Adding elements
uniqueNumbers.add(6);
uniqueFruits.addAll(['grape', 'mango']);
// Removing elements
uniqueNumbers.remove(3);
// Set operations
var setA = {1, 2, 3, 4};
var setB = {3, 4, 5, 6};
print(setA.union(setB)); // {1, 2, 3, 4, 5, 6}
print(setA.intersection(setB)); // {3, 4}
print(setA.difference(setB)); // {1, 2}
// Checking membership
print(uniqueFruits.contains('banana')); // true
// Iterating
for (var number in uniqueNumbers) {
print(number);
}
}
```
## 3. Maps
Maps are collections of key-value pairs.
### Example:
```dart
void main() {
// Creating maps
var ages = {'Alice': 30, 'Bob': 25, 'Charlie': 35};
Map<String, int> scores = {'Math': 90, 'Science': 85, 'History': 95};
// Accessing values
print(ages['Alice']); // 30
print(scores['Math']); // 90
// Adding or updating entries
ages['David'] = 28;
scores.addAll({'Geography': 88, 'Literature': 92});
// Removing entries
ages.remove('Bob');
// Checking keys and values
print(ages.containsKey('Charlie')); // true
print(scores.containsValue(100)); // false
// Iterating
ages.forEach((key, value) {
print('$key is $value years old');
});
// Getting all keys or values
print(scores.keys);
print(scores.values);
}
```
## 4. Collection Methods and Operations
Dart provides many useful methods for working with collections.
### Example:
```dart
void main() {
var numbers = [1, 2, 3, 4, 5];
// Functional methods
var doubled = numbers.map((n) => n * 2);
var evenNumbers = numbers.where((n) => n.isEven);
var sum = numbers.reduce((a, b) => a + b);
var product = numbers.fold(1, (a, b) => a * b);
print(doubled); // (2, 4, 6, 8, 10)
print(evenNumbers); // (2, 4)
print(sum); // 15
print(product); // 120
// Sorting
var fruits = ['banana', 'apple', 'orange'];
fruits.sort();
print(fruits); // [apple, banana, orange]
// Reversing
var reversedNumbers = numbers.reversed;
print(reversedNumbers); // (5, 4, 3, 2, 1)
// Shuffling
numbers.shuffle();
print(numbers); // Random order
// Finding elements
var firstEven = numbers.firstWhere((n) => n.isEven, orElse: () => -1);
print(firstEven);
}
```
## Conclusion
Dart's collection types provide powerful tools for managing groups of objects. Understanding how to effectively use Lists, Sets, and Maps, along with their associated methods and operations, is crucial for efficient Dart programming.