Menu
Coddy logo textTech

List, Set, Map Overview

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 63 of 110.

Dart provides three core collection types that you'll use constantly when building applications. Each serves a different purpose for organizing and accessing data.

A List is an ordered collection where elements are accessed by index. Items maintain their insertion order and duplicates are allowed:

var fruits = ['apple', 'banana', 'apple'];
print(fruits[0]);      // apple
print(fruits.length);  // 3

A Set is an unordered collection of unique elements. If you try to add a duplicate, it's simply ignored:

var uniqueNumbers = {1, 2, 3, 2, 1};
print(uniqueNumbers);  // {1, 2, 3}

A Map stores key-value pairs, letting you look up values by their associated key:

var ages = {'Alice': 30, 'Bob': 25};
print(ages['Alice']);  // 30

Here's a quick comparison:

CollectionOrderedDuplicatesAccess By
ListYesAllowedIndex
SetNoNot allowedContains check
MapNo*Unique keysKey

These collections become even more powerful when combined with generics, which you'll explore in the upcoming lessons to create type-safe collections that work with your custom classes.

challenge icon

Challenge

Easy

Let's build a classroom management system that uses all three collection types to organize student data in different ways. You'll see how Lists, Sets, and Maps each serve distinct purposes when managing the same information.

You'll organize your code into two files:

  • classroom.dart: Create a Classroom class that manages student data using all three collection types:
    • A List<String> called attendanceOrder to track students in the order they arrived (duplicates allowed if a student signs in multiple times)
    • A Set<String> called uniqueStudents to track which students are present (no duplicates)
    • A Map<String, int> called scores to store each student's test score
    Your Classroom should have these methods:
    • recordArrival(String name) - adds the name to both the attendance list and the unique students set
    • setScore(String name, int score) - stores the student's score in the map
    • printReport() - prints the classroom report (format shown below)
  • main.dart: Import your classroom file and demonstrate the system:
    • Create a Classroom instance
    • Record arrivals in this order: 'Alice', 'Bob', 'Alice', 'Carol', 'Bob'
    • Set scores: Alice = 95, Bob = 87, Carol = 92
    • Call printReport()

The printReport() method should output:

Arrival Order: [Alice, Bob, Alice, Carol, Bob]
Unique Students: {Alice, Bob, Carol}
Scores: {Alice: 95, Bob: 87, Carol: 92}

Notice how the List preserves every arrival (including duplicates), the Set automatically keeps only unique names, and the Map associates each student with their score. Each collection type serves its purpose perfectly!

Expected output:

Arrival Order: [Alice, Bob, Alice, Carol, Bob]
Unique Students: {Alice, Bob, Carol}
Scores: {Alice: 95, Bob: 87, Carol: 92}

Cheat sheet

Dart provides three core collection types for organizing data:

List - An ordered collection where elements are accessed by index. Duplicates are allowed:

var fruits = ['apple', 'banana', 'apple'];
print(fruits[0]);      // apple
print(fruits.length);  // 3

Set - An unordered collection of unique elements. Duplicates are automatically ignored:

var uniqueNumbers = {1, 2, 3, 2, 1};
print(uniqueNumbers);  // {1, 2, 3}

Map - Stores key-value pairs for lookup by key:

var ages = {'Alice': 30, 'Bob': 25};
print(ages['Alice']);  // 30
CollectionOrderedDuplicatesAccess By
ListYesAllowedIndex
SetNoNot allowedContains check
MapNo*Unique keysKey

Try it yourself

import 'classroom.dart';

void main() {
  // TODO: Create a Classroom instance
  
  // TODO: Record arrivals in this order: 'Alice', 'Bob', 'Alice', 'Carol', 'Bob'
  
  // TODO: Set scores: Alice = 95, Bob = 87, Carol = 92
  
  // TODO: Call printReport() to display the classroom data
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming