Menu
Coddy logo textTech

Nested Maps

Part of the Logic & Flow section of Coddy's Dart journey — lesson 31 of 65.

Sometimes you need to organize data in a more complex, hierarchical structure than a simple key-value pair. Nested maps allow you to store maps as values within other maps, creating multiple levels of data organization.

A nested map is simply a Map where the values are themselves Map objects. This structure is perfect for representing data that has categories and subcategories, such as user profiles with multiple details, or product catalogs with various attributes.

Map<String, Map<String, dynamic>> userProfiles = {
  'alice': {
    'email': 'alice@example.com',
    'age': 28
  },
  'bob': {
    'email': 'bob@example.com',
    'age': 32
  }
};

print(userProfiles['alice']['email']);  // alice@example.com
print(userProfiles['bob']['age']);      // 32

To access data in a nested map, you use multiple square brackets. The first set accesses the outer map's key, and the second set accesses the inner map's key. This two-level structure makes it easy to group related information together while keeping it organized and accessible.

challenge icon

Challenge

Easy

Create a program that manages a company's employee directory system using nested maps to store detailed employee information. Your program should:

  1. Read a string input representing the company name
  2. Read multiple sets of inputs representing employee data (the input will end when you receive "employees_done"). Each employee will have:
    • Employee ID (string)
    • Name (string)
    • Department (string)
    • Salary (string that needs to be converted to integer)
    • Years of experience (string that needs to be converted to integer)
  3. Read a string input representing an employee ID to look up
  4. Create a nested Map where each employee ID is a key, and the value is another Map containing the employee's details
  5. Access and display specific employee information using nested map access
  6. Calculate department statistics and print the employee directory report in the exact format shown below

For example, if the company name is "TechCorp Solutions", the employee data includes "EMP001" with name "Alice Johnson", department "Engineering", salary 75000, experience 5, "EMP002" with name "Bob Smith", department "Marketing", salary 60000, experience 3, "EMP003" with name "Carol Davis", department "Engineering", salary 80000, experience 7, and the employee ID to look up is "EMP001", your program should output:

Company: TechCorp Solutions
Employee Directory: {EMP001: {name: Alice Johnson, department: Engineering, salary: 75000, experience: 5}, EMP002: {name: Bob Smith, department: Marketing, salary: 60000, experience: 3}, EMP003: {name: Carol Davis, department: Engineering, salary: 80000, experience: 7}}
Employee Lookup (EMP001):
Name: Alice Johnson
Department: Engineering
Salary: $75000
Experience: 5 years
Department Analysis:
Engineering employees: 2
Marketing employees: 1
Total employees: 3
Average salary: $71666
Status: Employee directory processed successfully

If the company name is "Global Industries", the employee data includes "E100" with name "David Wilson", department "Sales", salary 55000, experience 4, "E101" with name "Emma Brown", department "HR", salary 65000, experience 6, and the employee ID to look up is "E101", your program should output:

Company: Global Industries
Employee Directory: {E100: {name: David Wilson, department: Sales, salary: 55000, experience: 4}, E101: {name: Emma Brown, department: HR, salary: 65000, experience: 6}}
Employee Lookup (E101):
Name: Emma Brown
Department: HR
Salary: $65000
Experience: 6 years
Department Analysis:
Sales employees: 1
HR employees: 1
Total employees: 2
Average salary: $60000
Status: Employee directory processed successfully

If the company name is "StartupHub", the employee data includes "S001" with name "Frank Miller", department "Development", salary 70000, experience 2, "S002" with name "Grace Lee", department "Design", salary 58000, experience 3, "S003" with name "Henry Chen", department "Development", salary 72000, experience 4, "S004" with name "Ivy Taylor", department "Marketing", salary 62000, experience 5, and the employee ID to look up is "S003", your program should output:

Company: StartupHub
Employee Directory: {S001: {name: Frank Miller, department: Development, salary: 70000, experience: 2}, S002: {name: Grace Lee, department: Design, salary: 58000, experience: 3}, S003: {name: Henry Chen, department: Development, salary: 72000, experience: 4}, S004: {name: Ivy Taylor, department: Marketing, salary: 62000, experience: 5}}
Employee Lookup (S003):
Name: Henry Chen
Department: Development
Salary: $72000
Experience: 4 years
Department Analysis:
Development employees: 2
Design employees: 1
Marketing employees: 1
Total employees: 4
Average salary: $65500
Status: Employee directory processed successfully

Your program must create a nested Map structure where the outer map uses employee IDs as keys, and each value is an inner map containing "name", "department", "salary", and "experience" as keys. Use nested map access with double square brackets to retrieve specific employee information. Count employees by department and calculate the average salary by dividing the total salary sum by the number of employees. The input format will be: company name, then sets of five values (employee ID, name, department, salary, experience) ending with "employees_done", followed by the employee ID to look up.

Cheat sheet

Nested maps allow you to store maps as values within other maps, creating hierarchical data structures:

Map<String, Map<String, dynamic>> userProfiles = {
  'alice': {
    'email': 'alice@example.com',
    'age': 28
  },
  'bob': {
    'email': 'bob@example.com',
    'age': 32
  }
};

Access nested map data using multiple square brackets:

print(userProfiles['alice']['email']);  // alice@example.com
print(userProfiles['bob']['age']);      // 32

The first bracket accesses the outer map's key, the second bracket accesses the inner map's key.

Try it yourself

import 'dart:io';

void main() {
  // Read company name
  String? companyName = stdin.readLineSync();
  
  // Initialize nested map for employee directory
  Map<String, Map<String, dynamic>> employeeDirectory = {};
  
  // Read employee data until "employees_done"
  while (true) {
    String? input = stdin.readLineSync();
    if (input == "employees_done") {
      break;
    }
    
    String employeeId = input!;
    String? name = stdin.readLineSync();
    String? department = stdin.readLineSync();
    String? salaryStr = stdin.readLineSync();
    String? experienceStr = stdin.readLineSync();
    
    // Convert salary and experience to integers
    int salary = int.parse(salaryStr!);
    int experience = int.parse(experienceStr!);
    
    // TODO: Create nested map structure for each employee and add to directory
    
  }
  
  // Read employee ID to look up
  String? lookupId = stdin.readLineSync();
  
  // TODO: Write your code below to:
  // 1. Display company name and employee directory
  // 2. Look up and display specific employee information
  // 3. Calculate department statistics
  // 4. Calculate average salary
  // 5. Print the complete report in the required format
  
}
quiz iconTest yourself

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

All lessons in Logic & Flow