从映射中删除条目
本课属于 Coddy Dart 学习旅程的“逻辑与流程控制”单元。第 30 课,共 65 课。
在管理 Map 中的数据时,你经常需要删除不再需要的条目。remove() 方法可以通过键直接从映射中删除键值对。
remove() 方法只接受一个参数:你想删除的条目的键。调用该方法时,它会从映射中同时删除键及其关联的值。如果映射中不存在该键,该方法不会执行任何操作,也不会导致错误。
Map<String, double> inventory = {
'Laptop': 999.99,
'Mouse': 25.50,
'Keyboard': 75.00
};
// 从 inventory 中移除鼠标
inventory.remove('Mouse');
print(inventory); // {Laptop: 999.99, Keyboard: 75.0}
// 尝试移除不存在的商品,不会产生任何效果
inventory.remove('Tablet');
print(inventory); // {Laptop: 999.99, Keyboard: 75.0}这个方法对于在应用中维护整洁且最新的映射至关重要。无论你是在管理库存、用户数据,还是其他需要移除项目的集合,remove() 都能让你安全、高效地按键删除条目。
挑战
简单创建一个程序,通过跟踪歌曲库并移除因授权问题而无法播放的歌曲,来管理数字音乐流媒体服务。你的程序应当:
- 读取一个字符串输入,表示流媒体服务的名称
- 读取多组输入,每组表示歌曲名称和播放次数(收到
"library_done"时结束输入) - 读取多个字符串输入,表示因授权问题而要移除的歌曲(收到
"removal_done"时结束输入) - 创建一个 Map,以歌曲名称为键,以播放次数为值
- 使用
remove()方法从歌曲库中移除指定的每首歌曲 - 计算歌曲库统计信息,并严格按照下方所示的格式打印移除报告
例如,如果服务名称是 "MusicStream Pro",歌曲库中包含播放次数为 2500000 的 "Bohemian Rhapsody"、播放次数为 1800000 的 "Imagine"、播放次数为 2200000 的 "Hotel California"、播放次数为 1900000 的 "Stairway to Heaven",要移除的歌曲是 "Imagine"、"Yesterday"、"Hotel California",那么你的程序应输出:
Service: MusicStream Pro
Original Library: {Bohemian Rhapsody: 2500000, Imagine: 1800000, Hotel California: 2200000, Stairway to Heaven: 1900000}
Removal Process:
Removing 'Imagine': Song removed (had 1800000 streams)
Removing 'Yesterday': Song not found in library
Removing 'Hotel California': Song removed (had 2200000 streams)
Updated Library: {Bohemian Rhapsody: 2500000, Stairway to Heaven: 1900000}
Library Statistics:
Songs before removal: 4
Songs after removal: 2
Songs successfully removed: 2
Songs not found: 1
Total streams lost: 4000000
Remaining total streams: 4400000
Status: Library update completed如果服务名称是 "SoundWave",歌曲库中包含播放次数为 3200000 的 "Shape of You"、播放次数为 2800000 的 "Blinding Lights"、播放次数为 2100000 的 "Someone Like You",要移除的歌曲是 "Blinding Lights"、"Perfect",那么你的程序应输出:
Service: SoundWave
Original Library: {Shape of You: 3200000, Blinding Lights: 2800000, Someone Like You: 2100000}
Removal Process:
Removing 'Blinding Lights': Song removed (had 2800000 streams)
Removing 'Perfect': Song not found in library
Updated Library: {Shape of You: 3200000, Someone Like You: 2100000}
Library Statistics:
Songs before removal: 3
Songs after removal: 2
Songs successfully removed: 1
Songs not found: 1
Total streams lost: 2800000
Remaining total streams: 5300000
Status: Library update completed如果服务名称是 "AudioHub",歌曲库中包含播放次数为 1500000 的 "Bad Guy"、播放次数为 1700000 的 "Watermelon Sugar"、播放次数为 1400000 的 "Levitating"、播放次数为 1600000 的 "Good 4 U",要移除的歌曲是 "Bad Guy"、"Levitating"、"Good 4 U"、"Drivers License",那么你的程序应输出:
Service: AudioHub
Original Library: {Bad Guy: 1500000, Watermelon Sugar: 1700000, Levitating: 1400000, Good 4 U: 1600000}
Removal Process:
Removing 'Bad Guy': Song removed (had 1500000 streams)
Removing 'Levitating': Song removed (had 1400000 streams)
Removing 'Good 4 U': Song removed (had 1600000 streams)
Removing 'Drivers License': Song not found in library
Updated Library: {Watermelon Sugar: 1700000}
Library Statistics:
Songs before removal: 4
Songs after removal: 1
Songs successfully removed: 3
Songs not found: 1
Total streams lost: 4500000
Remaining total streams: 1700000
Status: Library update completed你的程序必须使用 remove() 方法从 Map 中删除歌曲。跟踪哪些歌曲已成功移除,哪些歌曲未找到。将成功移除歌曲的播放次数相加,计算播放次数损失总和。输入格式为:服务名称,然后是交替出现的歌曲名称和播放次数(以需要转换为整数的字符串形式输入),直到 "library_done" 为止;接着输入要移除的歌曲名称,直到 "removal_done" 为止。
亲手试试
import 'dart:io';
void main() {
// 读取服务名称
String? serviceName = stdin.readLineSync();
// 创建一个 Map,存储歌曲标题和播放次数
Map<String, int> songLibrary = {};
// 读取歌曲库数据
while (true) {
String? input = stdin.readLineSync();
if (input == "library_done") break;
String songTitle = input!;
String? streamCountStr = stdin.readLineSync();
int streamCount = int.parse(streamCountStr!);
songLibrary[songTitle] = streamCount;
}
// 读取要移除的歌曲
List<String> songsToRemove = [];
while (true) {
String? input = stdin.readLineSync();
if (input == "removal_done") break;
songsToRemove.add(input!);
}
// TODO:在下方编写你的代码
// 使用 remove() 方法移除歌曲
// 记录统计数据,并按要求的格式生成输出
// 按要求的格式打印结果
// 记得包含:服务名称、原始歌曲库、移除过程、
// 更新后的歌曲库和歌曲库统计数据
}这节课包含一个小测验。开始学习即可作答,并记录你的进度。
逻辑与流程控制中的所有课
自己练习: Dart 在线编译器