Dart

What are the different types of collections in Dart


Dart provides a rich set of collection types that allow you to store and manipulate groups of objects. Collections are essential for managing data efficiently and can be categorized into three main types: Lists, Sets, and Maps. Each collection type has its own characteristics and use cases.

1. Lists

A List is an ordered collection of items that can contain duplicate elements. Lists are indexed, meaning you can access elements by their position in the list. Dart provides two types of lists: List and Fixed-length List.

Example of a List

void main() {
    // Creating a list of integers
    List<int> numbers = [1, 2, 3, 4, 5];
    // Adding an element
    numbers.add(6);
    // Accessing elements
    print(numbers[0]); // Output: 1
    // Iterating through the list
    for (var number in numbers) {
        print(number); // Output: 1 2 3 4 5 6
    }
}
</int>

In this example, we create a list of integers, add an element to it, access an element by its index, and iterate through the list to print its contents.

2. Sets

A Set is an unordered collection of unique items. Sets do not allow duplicate elements, making them useful for storing distinct values. Dart provides two types of sets: Set and LinkedHashSet.

Example of a Set

void main() {
    // Creating a set of strings
    Set<string> fruits = {'apple', 'banana', 'orange'};
    // Adding an element
    fruits.add('grape');
    // Trying to add a duplicate element
    fruits.add('apple'); // This will not be added
    // Iterating through the set
    for (var fruit in fruits) {
        print(fruit); // Output: apple banana orange grape (order may vary)
    }
}
</string>

In this example, we create a set of strings, add a new element, and attempt to add a duplicate element. The duplicate is not added, and we iterate through the set to print its contents.

3. Maps

A Map is a collection of key-value pairs, where each key is unique. Maps are useful for associating values with keys, allowing for efficient data retrieval. Dart provides a Map class that can be used to create maps with various key and value types.

Example of a Map

void main() {
    // Creating a map of string keys and integer values
    Map<string, int> scores = {
        'Alice': 90,
        'Bob': 85,
        'Charlie': 92,
    };
    // Adding a new key-value pair
    scores['David'] = 88;
    // Accessing a value by key
    print(scores['Alice']); // Output: 90
    // Iterating through the map
    scores.forEach((key, value) {
        print('$key: $value'); // Output: Alice: 90, Bob: 85, Charlie: 92, David: 88
    });
}
</string,>

In this example, we create a map that associates names with scores, add a new key-value pair, access a value by its key, and iterate through the map to print its contents.

4. Summary of Collection Types

  • List: An ordered collection of items that can contain duplicates. Accessed by index.
  • Set: An unordered collection of unique items. Does not allow duplicates.
  • Map: A collection of key-value pairs, where each key is unique. Accessed by key.

5. Conclusion

Dart provides a variety of collection types to manage data effectively. Understanding the differences between lists, sets, and maps, as well as their use cases, is essential for writing efficient and organized Dart applications. By leveraging these collection types, you can handle data in a way that best suits your application's needs.

Written by Surfside Media

Senior Full Stack Developer specializing in Web Technologies.