Dart

14. Collections

                               Dart Collections

In Dart, collections are used to store multiple values of similar or different types. Dart provides several built-in collection types that serve different purposes, such as lists, sets, and maps. Let’s explore each of these collection types with examples:

1. Lists

Lists in Dart are ordered collections of elements, where each element is indexed starting from 0. Lists can grow or shrink dynamically.

Example:

void main() {
  // Creating a list of integers
  List<int> numbers = [1, 2, 3, 4, 5];

  // Accessing elements of the list
  print('First element: ${numbers[0]}');   // Output: First element: 1
  print('Length of the list: ${numbers.length}'); // Output: Length of the list: 5

  // Iterating over a list using for-in loop
  for (var number in numbers) {
    print('Number: $number');
  }

  // Adding elements to the list
  numbers.add(6);
  numbers.addAll([7, 8]);
  print('Updated list: $numbers'); // Output: Updated list: [1, 2, 3, 4, 5, 6, 7, 8]

  // Removing elements from the list
  numbers.remove(3);
  print('List after removal: $numbers'); // Output: List after removal: [1, 2, 4, 5, 6, 7, 8]

  // Checking if a list contains an element
  bool containsSeven = numbers.contains(7);
  print('Does the list contain 7? $containsSeven'); // Output: Does the list contain 7? true
}

2. Sets

Sets in Dart are unordered collections of unique elements. Sets do not allow duplicate elements.

Example:

void main() {
  // Creating a set of strings
  Set<String> fruits = {'apple', 'banana', 'cherry'};

  // Adding elements to the set
  fruits.add('orange');
  print('Updated set: $fruits'); // Output: Updated set: {apple, banana, cherry, orange}

  // Adding duplicate element (ignored in set)
  fruits.add('banana');
  print('Set after adding duplicate: $fruits'); // Output: Set after adding duplicate: {apple, banana, cherry, orange}

  // Removing an element from the set
  fruits.remove('banana');
  print('Set after removal: $fruits'); // Output: Set after removal: {apple, cherry, orange}

  // Checking if a set contains an element
  bool containsApple = fruits.contains('apple');
  print('Does the set contain apple? $containsApple'); // Output: Does the set contain apple? true
}

3. Maps

Maps in Dart are collections of key-value pairs, where each key is unique. Maps are useful for associating keys with values.

Example:

void main() {
  // Creating a map of student names and their ages
  Map<String, int> studentAges = {
    'Alice': 25,
    'Bob': 30,
    'Carol': 28,
  };

  // Accessing values from the map using keys
  print('Age of Alice: ${studentAges['Alice']}'); // Output: Age of Alice: 25

  // Adding a new entry to the map
  studentAges['David'] = 22;
  print('Updated map: $studentAges'); // Output: Updated map: {Alice: 25, Bob: 30, Carol: 28, David: 22}

  // Removing an entry from the map
  studentAges.remove('Bob');
  print('Map after removal: $studentAges'); // Output: Map after removal: {Alice: 25, Carol: 28, David: 22}

  // Checking if a map contains a key
  bool containsCarol = studentAges.containsKey('Carol');
  print('Does the map contain Carol? $containsCarol'); // Output: Does the map contain Carol? true
}

Summary

  • Lists are ordered collections that allow duplicate elements and can grow or shrink dynamically.
  • Sets are unordered collections of unique elements, useful when uniqueness is required.
  • Maps are collections of key-value pairs where keys are unique, allowing efficient lookup and association of values with keys.

Leave a Reply

Your email address will not be published. Required fields are marked *