Thuta Learning
Dart
BasicProgrammingအခြေခံ

Collections (Maps)

Dartသင်ခန်းစာ 1040 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Type ပါတဲ့ Map ကို ဖန်တီးပြီး ဖတ်၊ ရေးနိုင်ကာ map[key] က V? ပြန်တာကို နားလည်ဖို့
  • containsKey, putIfAbsent, update (ifAbsent နဲ့) နဲ့ remove ကို သုံးနိုင်ဖို့
  • keys, values, entries နဲ့ iterate လုပ်ပြီး map နဲ့ collection-for နဲ့ map အသစ် ဆောက်နိုင်ဖို့
  • Default map ရဲ့ insertion order၊ SplayTreeMap, unmodifiable map တို့ကို ခွဲခြားနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

Map ဆိုတာ အဘိဓာန် ဒါမှမဟုတ် ဖုန်းထဲက contact စာရင်းလိုပါပဲ။ စာမျက်နှာ 37 ကို လှန်ကြည့်စရာ မလိုဘဲ နာမည် (key) ကို ရှာလိုက်ရင် အဲဒီနာမည်နဲ့ တွဲထားတဲ့ ဖုန်းနံပါတ် (value) ကို တန်းရပါတယ်။ Key တစ်ခုကို value တစ်ခုပဲ ပိုင်ဆိုင်ပြီး key တူရင် value အသစ်က အဟောင်းကို အစားထိုးပါတယ်။

List မှာ ဘောက်စ်နံပါတ်နဲ့ ရှာပေမယ့် Map မှာတော့ ကိုယ်ကိုယ်တိုင် ရွေးတဲ့ key (String ဖြစ်ဖြစ်၊ int ဖြစ်ဖြစ်) နဲ့ ရှာပါတယ်။ ဒါကြောင့် user profile၊ ပစ္စည်းစာရင်း၊ setting တွေလို "နာမည်နဲ့ ခေါ်ချင်တဲ့ data" အတွက် သင့်တော်ပါတယ်။

Problem → Why → Solution

ပြဿနာ: list နှစ်ခုကို အပြိုင် သုံးရင် လွဲသွားတတ်တယ်

ကစားသမားတွေရဲ့ နာမည်ကို list တစ်ခုထဲမှာ၊ အမှတ်ကို တခြား list တစ်ခုထဲမှာ ထားပြီး နေရာတူတာချင်း တွဲမှတ်ထားမယ်ဆိုပါစို့။ Kyaw ရဲ့ အမှတ်ကို ရှာချင်ရင် နာမည် list ထဲမှာ ရှာပြီး ရတဲ့ index နဲ့ အမှတ် list ကို ဖွင့်ရပါတယ်။ ဆိုးတာက list တစ်ခုကို ပြင်ပြီး ကျန်တစ်ခုကို မပြင်မိရင် data တွေက တိတ်တဆိတ် လွဲသွားတာပါ။ အောက်က code မှာ Aye ကို ဖယ်လိုက်ပြီးနောက် Kyaw ရဲ့ အမှတ်လို့ ထင်ရတာက တကယ်တော့ Aye ရဲ့ အမှတ် 120 ပါ။

parallel_lists.dart
void main() {
  List<String> names = ['Aye', 'Kyaw', 'Thida'];
  List<int> points = [120, 85, 97];

  // Find Kyaw's points: search the names, then reuse the index.
  int index = names.indexOf('Kyaw');
  print(points[index]);                     // 85

  // One list is edited and the other is forgotten.
  names.removeAt(0);
  print(points[names.indexOf('Kyaw')]);     // 120: Aye's points, silently wrong
}

Map က ဘာကို တားပေးလဲ

Map မှာ key နဲ့ value ကို တစ်ခုတည်းမှာ တွဲသိမ်းထားလို့ တစ်ဖက်ကို ဖယ်ရင် ကျန်တစ်ဖက်လည်း အတူပါသွားပါတယ်၊ ဒါကြောင့် ဒီလို လွဲတာမျိုး မဖြစ်နိုင်ပါဘူး။ Lookup လည်း index ရှာဖို့ list တစ်ခုလုံးကို လိုက်ဖတ်စရာ မလိုဘဲ points['Kyaw'] လို့ တိုက်ရိုက် ရေးလို့ရပြီး အလေ့အထအရ အရမ်းမြန်ပါတယ်။ ဒါပေမယ့် key မရှိရင် ဘာဖြစ်လဲ၊ value အမျိုးအစား ဘာလဲဆိုတာကို သတိထားရပါမယ်၊ ဒီ lesson က အဲဒါတွေကို ရှင်းပြပါမယ်။

Core concept: Map ရဲ့ type၊ null နဲ့ အစီအစဉ်

Map literal ကို { key: value } နဲ့ ရေးပြီး type ကို Map<String, int> လို့ ဖော်ပြပါတယ်။ {'a': 1, 'b': 2} ကို Dart က Map<String, int> လို့ ခန့်မှန်းပေးပါတယ်၊ value အမျိုးမျိုး ရောရင် {'name': 'Sai', 'points': 120} က Map<String, Object> ဖြစ်ပါတယ်။ var empty = {}; ကတော့ Map<dynamic, dynamic> ဖြစ်သွားလို့ <String, int>{} လို့ type ပေးပါ။ JSON လို data မှာ Map<String, dynamic> ကို အများကြီး တွေ့ရပါတယ်၊ ဒါပေမယ့် dynamic က value အမျိုးအစား စစ်ဆေးမှုကို ပိတ်လိုက်တာမို့ int p = user['points'] လို့ ရေးရင် compile ဖြစ်ပေမယ့် value က String ဆိုရင် run time မှာ "type 'String' is not a subtype of type 'int'" ဖြစ်ပါတယ်။

map[key] ရဲ့ ရလဒ်က value type မဟုတ်ဘဲ V? ပါ၊ key မရှိရင် error မပစ်ဘဲ null ပြန်ပါတယ်။ ဒါကြောင့် Map<String, int> stock မှာ int n = stock['tea']; လို့ ရေးရင် "A value of type 'int?' can't be assigned to a variable of type 'int'." ဆိုတဲ့ compile error ထွက်ပါတယ်။ ဖြေရှင်းနည်းတွေက stock['tea'] ?? 0 နဲ့ ပုံသေတန်ဖိုး ပေးတာ၊ containsKey နဲ့ အရင်စစ်တာ၊ null မဟုတ်ဘူးလို့ သေချာရင် ! သုံးတာပါ၊ ! က key မရှိရင် "Null check operator used on a null value" နဲ့ crash ဖြစ်ပါတယ်။ Value type က nullable (int?) ဖြစ်ရင် null ပြန်တာက key မရှိလို့လား၊ value က null လို့လားဆိုတာကို containsKey နဲ့ ခွဲခြားရပါမယ်။

ရေးတဲ့ အပိုင်းမှာ map[key] = value က ထည့်တာရော အစားထိုးတာရော လုပ်ပါတယ်။ remove(key) က ဖယ်လိုက်တဲ့ value ကို ပြန်ပေးပြီး key မရှိရင် null ပါ။ putIfAbsent(key, () => value) က key မရှိမှသာ function ကို ခေါ်ပြီး ထည့်ပါတယ်၊ ရလဒ်အနေနဲ့ map ထဲက value ကို ပြန်ပေးပါတယ်။ update(key, (v) => newValue) ကတော့ ရှိပြီးသား value ကို ပြောင်းဖို့ပါ၊ key မရှိရင် ifAbsent: () => value မပေးထားရင် ArgumentError ("Invalid argument (key): Key not in map.") ပစ်ပါတယ်။ addAll, removeWhere, clear တို့လည်း ရှိပါတယ်။

Iterate လုပ်ဖို့ keys, values နဲ့ entries ရှိပါတယ်၊ entries ကတာ့ MapEntry တွေ ပေးပြီး entry.key နဲ့ entry.value ကို သုံးပါတယ်။ forEach((k, v) => ...) လည်း ရပါတယ်။ Literal နဲ့ ဖန်တီးတဲ့ ပုံမှန် Map က insertion order ကို မှတ်ထားတဲ့ LinkedHashMap ပါ၊ ဒါကြောင့် key တွေကို ထည့်ခဲ့တဲ့ အစီအစဉ်အတိုင်း ပြန်ရပါတယ်၊ ရှိပြီးသား key ကို value ပြောင်းရင် နေရာမပြောင်းပါဘူး၊ ဖယ်ပြီး ပြန်ထည့်ရင်တော့ အဆုံးမှာ ရောက်ပါတယ်။ dart:collection ထဲက HashMap က order ကို အာမမခံပါဘူး၊ SplayTreeMap ကတော့ key တွေကို စီပြီး ထားပါတယ်။ Iterate လုပ်နေတုန်း key ကို ထည့်၊ ဖယ်ရင် ConcurrentModificationError ဖြစ်ပါတယ်။

Key တွေကို == နဲ့ hashCode နဲ့ နှိုင်းယှဉ်လို့ String နဲ့ int key တွေက ပြဿနာမရှိပေမယ့် ပြောင်းလဲနိုင်တဲ့ list ကို key အဖြစ် သုံးတာက အန္တရာယ်ရှိပါတယ်။ Map နှစ်ခုက content တူရင်တောင် == ဆိုတာ false ပါ၊ list လိုပဲ identity နဲ့ နှိုင်းလို့ပါ။ Map.unmodifiable(source) နဲ့ const {} က ပြင်လို့မရပါဘူး၊ ပြင်ရင် "Unsupported operation: Cannot modify unmodifiable map" ပါ။ Map အသစ်ကို {...other, 'c': 3}၊ {for (final e in ...) e.key: e.value} လို့ရော map.map((k, v) => MapEntry(k, v * 2)) နဲ့ပါ ဆောက်လို့ရပါတယ်။

အဘိဓာန်
  1. Key တစ်ခုချင်းစီက value တစ်ခုပိုင်
  2. map[key] က V? ပြန်၊ မရှိရင် null
  3. ထည့်ခဲ့တဲ့ အစီအစဉ်ကို မှတ်ထား
  4. Key တူရင် value အသစ်က အစားထိုး

Map ကို ဖတ်တိုင်း "ဒီ key မရှိရင် ဘာဖြစ်မလဲ" ဆိုတာ အရင်စဉ်းစားပါ၊ Dart က null ပြန်တာမို့ ကိုယ်တိုင် handle လုပ်ရပါမယ်။

အထွေထွေ ပုံစံ

dart
Map<KeyType, ValueType> name = {key1: value1, key2: value2};
var empty = <KeyType, ValueType>{};
name[key] = value;                       // add or replace
ValueType? found = name[key];            // null when the key is missing
name.putIfAbsent(key, () => value);      // add only if the key is missing
name.update(key, (v) => newValue, ifAbsent: () => value);
for (final entry in name.entries) { /* entry.key, entry.value */ }

Map ကို key type နဲ့ value type နှစ်ခုလုံးနဲ့ ကြေညာပါတယ်။ ဖတ်တဲ့ အခါ ရလဒ်က ValueType? ဖြစ်တာကို သတိပြုပါ။ putIfAbsent နဲ့ update က ရှိ/မရှိ စစ်ပြီး ထည့်တာ၊ ပြင်တာကို တစ်ဆင့်တည်းနဲ့ လုပ်ပေးပြီး entries က key နဲ့ value နှစ်ခုလုံးကို တစ်ပြိုင်နက် ပေးပါတယ်။

Example: user profile နဲ့ ပစ္စည်းစာရင်း

ဒီ example မှာ ပထမ Map<String, dynamic> က user profile ကို ကိုယ်စားပြုပြီး တန်ဖိုးအမျိုးမျိုး (String, int, bool) ပါပါတယ်။ ဖတ်တာ၊ ပြောင်းတာ၊ မရှိတဲ့ key ကို ဖတ်ရင် null ရတာကို ကြည့်ပါ။ ပြီးရင် value အားလုံး int ဖြစ်တဲ့ Map<String, int> stock ကို putIfAbsent နဲ့ update သုံးပြီး ပြင်ကာ entries နဲ့ ပုံနှိပ်ပါတယ်။

maps.dart
void main() {
  Map<String, dynamic> user = {
    'name': 'Sai',
    'role': 'Developer',
    'points': 120,
    'isActive': true,
  };

  print(user['name']);
  print(user['points']);

  user['points'] = 150;
  print('Updated points: ${user['points']}');

  // a missing key gives null, not an error
  print(user['email']);

  // a typed map: every value is an int
  Map<String, int> stock = {'tea': 5, 'rice': 2};
  stock.putIfAbsent('coffee', () => 9);
  stock.update('tea', (count) => count + 1);

  for (final entry in stock.entries) {
    print('${entry.key}: ${entry.value}');
  }
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Line 2Map<String, dynamic> user = {

    Key အားလုံးက String ပြီး value က dynamic ဖြစ်လို့ String, int, bool ရောနှောနိုင်ပါတယ်။ ဒီလို map က JSON ကနေ လာတဲ့ data မှာ အဖြစ်များပါတယ်။

  2. Line 12user['points'] = 150;

    'points' key ရှိပြီးသားမို့ value 120 ကို 150 နဲ့ အစားထိုးပါတယ်။ key အသစ်ဆိုရင် ထည့်ပေးမှာပါ။ နှစ်ခုလုံးအတွက် syntax အတူတူပါပဲ။

  3. Line 16print(user['email']);

    'email' key မရှိလို့ error မပစ်ဘဲ null ပြန်ပါတယ်။ ဒါကြောင့် print က null ကို ပြပါတယ်။

  4. Line 19Map<String, int> stock = {'tea': 5, 'rice': 2};

    Value အားလုံး int ဖြစ်တဲ့ typed map ပါ။ ဒီမှာ stock['tea'] ရဲ့ အမျိုးအစားက int? ဖြစ်ပြီး 'tea' ကို string တစ်ခု ထည့်ဖို့ ကြိုးစားရင် compile error ပါ။

  5. Line 20stock.putIfAbsent('coffee', () => 9);

    'coffee' key မရှိသေးလို့ function ကို ခေါ်ပြီး 9 ကို ထည့်ပါတယ်။ ရှိပြီးသားဆိုရင် function ကို လုံးဝ မခေါ်ဘဲ မူလ value ကို ထားပါတယ်။

  6. Line 21stock.update('tea', (count) => count + 1);

    ရှိပြီးသား 'tea' ရဲ့ value 5 ကို function ထဲ ပို့ပြီး ရလဒ် 6 ကို ပြန်သိမ်းပါတယ်။ 'tea' မရှိရင် ifAbsent မပါလို့ ArgumentError ပစ်ပါလိမ့်မယ်။

  7. Line 23for (final entry in stock.entries) {

    entries က key နဲ့ value နှစ်ခုလုံးကို MapEntry တစ်ခုအဖြစ် ပေးပါတယ်။ ထည့်ခဲ့တဲ့ အစီအစဉ် tea, rice, coffee အတိုင်း ထွက်ပါတယ်၊ tea ကို update လုပ်ပေမယ့် နေရာမပြောင်းပါဘူး။

You should see
Sai
120
Updated points: 150
null
tea: 6
rice: 2
coffee: 9

လုံခြုံစွာ ဖတ်ခြင်း: null, containsKey နဲ့ typed map

အောက်က code မှာ ပထမ ရေးမယ့် int n = stock['tea']; ကို comment ထဲ ထည့်ထားတာက compile error ဖြစ်လို့ပါ။ ?? 0 နဲ့ ပုံသေတန်ဖိုး ပေးတာ၊ containsKey နဲ့ အရင်စစ်တာ၊ int? variable ထဲ ထည့်ပြီး null မဟုတ်မှ သုံးတာ (အဲဒီ block ထဲမှာ type promotion ရပါတယ်) ဆိုတဲ့ နည်းသုံးမျိုးကို တွေ့နိုင်ပါတယ်။ နောက်ဆုံးမှာ entries.reduce နဲ့ value အများဆုံး ရှိတဲ့ entry ကို ရှာပါတယ်။

safe_reads.dart
void main() {
  Map<String, int> stock = {'tea': 5, 'rice': 2};

  // int n = stock['tea'];       // compile error: the result is int?
  int tea = stock['tea'] ?? 0;
  int coffee = stock['coffee'] ?? 0;
  print('$tea $coffee');

  if (stock.containsKey('rice')) {
    print('rice is listed');
  }

  int? maybe = stock['milk'];
  if (maybe != null) {
    print(maybe + 1);              // promoted to int inside this block
  } else {
    print('no milk');
  }

  var scores = {'aye': 80, 'kyaw': 65};
  var best = scores.entries.reduce((a, b) => a.value >= b.value ? a : b);
  print(best.key);
}

ရေတွက်ခြင်း၊ အုပ်စုခွဲခြင်းနဲ့ အစီအစဉ်

Map အမျိုးအစားရယူနည်းKey အစီအစဉ်
ပုံမှန် (LinkedHashMap){ } literal, Map.of(x)ထည့်ခဲ့တဲ့ အစီအစဉ်
HashMapdart:collectionအာမခံချက် မရှိ
SplayTreeMapdart:collectionKey စီထားတဲ့ အစီအစဉ်

ရေတွက်တာနဲ့ အုပ်စုခွဲတာက Map ရဲ့ အသုံးအများဆုံး pattern နှစ်ခုပါ။ update နဲ့ ifAbsent ကို တွဲပြီး စကားလုံးတစ်ခုချင်းစီရဲ့ အကြိမ်ရေကို တိုးပါတယ်။ putIfAbsent(key, () => []) က key မရှိရင် list အလွတ်ကို ဖန်တီးပေးပြီး ရလဒ်ကို တန်းပြီး .add လုပ်လို့ရပါတယ်။ map.map(...) ကတော့ map အသစ်တစ်ခု ပြန်ပေးပြီး SplayTreeMap.from က key တွေကို စီပေးပါတယ်။

counting.dart
import 'dart:collection';

void main() {
  var words = ['to', 'be', 'or', 'not', 'to', 'be'];
  var counts = <String, int>{};
  for (final w in words) {
    counts.update(w, (n) => n + 1, ifAbsent: () => 1);
  }
  print(counts);

  var byLength = <int, List<String>>{};
  for (final w in ['fig', 'pear', 'kiwi', 'plum', 'yam']) {
    byLength.putIfAbsent(w.length, () => []).add(w);
  }
  print(byLength);

  var doubled = counts.map((word, n) => MapEntry(word, n * 2));
  print(doubled);

  var sorted = SplayTreeMap<String, int>.from(counts);
  print(sorted);
}

Iterate လုပ်နေတုန်း map ကို မပြင်ပါနဲ့

for (final k in map.keys) { map.remove(k); } လို့ ရေးရင် ConcurrentModificationError ဖြစ်ပါတယ်။ ဖယ်ချင်တဲ့ condition ရှိရင် map.removeWhere((k, v) => v > 20) လို့ ရေးပါ၊ ဒါမှမဟုတ် key list ကို ကူးယူပြီး (map.keys.toList()) အဲဒီ list ကို loop လုပ်ပါ။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Server ကပို့တဲ့ JSON ကို jsonDecode နဲ့ ဖတ်ရင် Map<String, dynamic> တွေ ရပါတယ်၊ ဒါပေမယ့် app ရဲ့ အတွင်းပိုင်းမှာ အဲဒီ map ကို User လို class (နောက် lesson တွေမှာ သင်ပါမယ်) အဖြစ် အမြန်ဆုံး ပြောင်းပြီး typed data နဲ့ အလုပ်လုပ်ကြပါတယ်။ Flutter မှာ routes: {'/home': (context) => HomePage()} လို route table က Map တစ်ခုပါ၊ setting တွေ၊ translation string တွေ၊ HTTP header တွေလည်း Map ပါပဲ။

Cache လုပ်ဖို့ cache.putIfAbsent(id, () => expensiveLoad(id)) လို့ ရေးတာက တူညီတဲ့ id ကို ထပ်ခေါ်ရင် ပြန်တွက်ခြင်းမရှိအောင် လုပ်ပေးပါတယ်။ Analytics မှာ event တစ်ခုချင်းစီကို counts.update(name, (n) => n + 1, ifAbsent: () => 1) နဲ့ ရေတွက်ပြီး ပစ္စည်းတွေကို category အလိုက် အုပ်စုခွဲတာမှာ putIfAbsent(category, () => []).add(item) ကို သုံးပါတယ်။ Test တွေမှာလည်း အသုံးပြုသူ ပို့လိုက်တဲ့ request body ကို Map နဲ့ ရေးပြီး ပြန်လာတဲ့ ရလဒ်နဲ့ ယှဉ်ကြပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. map[key] က null မဖြစ်ဘူးလို့ ယူဆတာ

Map<String, int> stock မှာ int n = stock['tea']; ဆိုရင် "A value of type 'int?' can't be assigned to a variable of type 'int'." လို့ ပေါ်ပါတယ်။ ! ကို ပိတ်ဖို့ ထည့်လိုက်ရင် stock['coffee']! + 1 က key မရှိတဲ့အခါ "Null check operator used on a null value" နဲ့ ရပ်သွားပါတယ်။ ?? 0 ဒါမှမဟုတ် containsKey နဲ့ ကိုင်တွယ်ပါ။

2. key မရှိတဲ့အခါ update ကို ifAbsent မပါဘဲ သုံးတာ

Map<String, int> m = {'a': 1}; m.update('b', (v) => v + 1); ကို run ရင် ArgumentError က "Invalid argument (key): Key not in map." နဲ့ စတဲ့ message ကို ပေးပြီး မရှိတဲ့ key 'b' ကိုပါ ထည့်ပြပါတယ်။ ရေတွက်တဲ့ code မှာ update ကို ifAbsent: () => 1 နဲ့ တွဲပါ၊ ဒါမှမဟုတ် m['b'] = (m['b'] ?? 0) + 1 လို့ ရေးပါ။

3. dynamic ရဲ့ နောက်ကွယ်မှာ type အမှားကို ဝှက်တာ

Map<String, dynamic> user ထဲက points ကို int p = user['points']; လို့ ယူရင် compile ဖြစ်ပေမယ့် value က String ဖြစ်နေရင် run time မှာ ကွဲပါတယ်။ var user = {'name': 'Sai', 'points': 120}; လို့ ရေးရင်တော့ Map<String, Object> ဖြစ်ပြီး user['points'] ရဲ့ type က Object? ဖြစ်လို့ "A value of type 'Object?' can't be assigned to a variable of type 'int'." ဆိုတဲ့ compile error ပေါ်ပါတယ်။ is စစ်ချက် ဒါမှမဟုတ် typed class နဲ့ ဖြေရှင်းပါ။

4. Iterate လုပ်နေတုန်း key ကို ဖယ်တာ

for (final k in order.keys) { order.remove(k); } က ConcurrentModificationError ကို ပစ်ပါတယ်။ ပထမ element ကို ဖယ်လိုက်တာနဲ့ map ရဲ့ အတွင်းပိုင်း ပြောင်းသွားပြီး loop က ဆက်သွားလို့ မရတော့လို့ပါ။ removeWhere ကို သုံးပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • ဖြစ်နိုင်ရင် Map<String, int> လို တိကျတဲ့ type ကို သုံးပါ၊ Map<String, dynamic> ကို JSON လို နယ်စပ်မှာပဲ ထားပြီး အတွင်းမှာ class အဖြစ် ပြောင်းပါ။
  • map[key]! အစား map[key] ?? default ကို ရွေးပါ၊ key မရှိတာက ပုံမှန် အခြေအနေဆိုရင် ဒီပုံစံက crash မဖြစ်စေပါဘူး။
  • key နဲ့ value နှစ်ခုလုံး လိုရင် keys ကို loop လုပ်ပြီး ပြန်ရှာတာထက် entries ကို loop လုပ်ပါ။
  • ရေတွက်တာနဲ့ အုပ်စုခွဲတာအတွက် update နဲ့ putIfAbsent ကို သုံးပါ၊ ရှိ/မရှိ if နဲ့ စစ်တာထက် တိုပြီး ရှင်းပါတယ်။

Mini challenge

Mini challenge: စကားလုံး ရေတွက်ခြင်းနဲ့ ပစ္စည်းဝင်ခြင်း

Map helper သုံးခု ရေးပါ၊ စာသားထဲက စကားလုံးတွေကို ရေတွက်တာ၊ အများဆုံးတွေ့တဲ့ စကားလုံးကို ရှာတာ၊ ပစ္စည်းအသစ်ဝင်လာတဲ့အခါ မူလ stock ကို မပြောင်းဘဲ map အသစ် ထုတ်ပေးတာတို့ ပါပါတယ်။

  • Map<String, int> countWords(String text) — စာလုံးအသေးဖြစ်အောင် ပြောင်းပြီး space နဲ့ ခွဲကာ update နဲ့ ifAbsent သုံးပြီး ရေတွက်ရမယ်
  • String mostCommon(Map<String, int> counts) — count အများဆုံး key ကို ပြန်ရမယ်၊ map အလွတ်ဆိုရင် 'none' ပြန်ရမယ်
  • Map<String, int> restock(Map<String, int> stock, Map<String, int> delivery) — stock ကို ကူးပြီး delivery ပမာဏတွေ ပေါင်းထည့်ကာ map အသစ် ပြန်ရမယ်၊ မူလ stock မပြောင်းရ
  • main မှာ 'Tea tea rice Tea rice coffee' ကို ရေတွက်ပြီး အများဆုံးစကားလုံးကို print ထုတ်ပါ၊ ပြီးရင် restock ရလဒ်နဲ့ မူလ stock ကို print ထုတ်ပါ
map_helpers.dart
Map<String, int> countWords(String text) {
  // TODO: lowercase, split on spaces, count with update and ifAbsent
}

String mostCommon(Map<String, int> counts) {
  // TODO: find the key with the highest count; 'none' when the map is empty
}

Map<String, int> restock(Map<String, int> stock, Map<String, int> delivery) {
  // TODO: copy stock first, then add each delivery amount to the copy
}

void main() {
  // TODO: count 'Tea tea rice Tea rice coffee', print counts and the top word
  // TODO: restock {'tea': 2, 'rice': 1} with {'rice': 4, 'coffee': 3}
  // TODO: print the new map and then the original stock
}

ရလဒ်: ထွက်ရမယ့် ရလဒ်- {tea: 3, rice: 2, coffee: 1} tea {tea: 2, rice: 5, coffee: 3} {tea: 2, rice: 1}

အရိပ်အမြွက် ကြည့်ရန်

restock မှာ Map<String, int>.of(stock) နဲ့ ကူးပြီးမှ ပြင်ပါ၊ delivery.forEach((item, qty) { ... }) ထဲမှာ update ကို ifAbsent နဲ့ တွဲပါ။ mostCommon အတွက် counts.entries.reduce က entry နှစ်ခုကို နှိုင်းယှဉ်ပြီး တစ်ခုကို ရွေးပေးပါတယ်၊ ပြီးရင် .key ကို ယူပါ။

အဖြေ ကြည့်ရန်
map_helpers.dart
Map<String, int> countWords(String text) {
  final counts = <String, int>{};
  for (final word in text.toLowerCase().split(' ')) {
    counts.update(word, (n) => n + 1, ifAbsent: () => 1);
  }
  return counts;
}

String mostCommon(Map<String, int> counts) {
  if (counts.isEmpty) {
    return 'none';
  }
  return counts.entries.reduce((a, b) => b.value > a.value ? b : a).key;
}

Map<String, int> restock(Map<String, int> stock, Map<String, int> delivery) {
  final result = Map<String, int>.of(stock);
  delivery.forEach((item, qty) {
    result.update(item, (current) => current + qty, ifAbsent: () => qty);
  });
  return result;
}

void main() {
  final counts = countWords('Tea tea rice Tea rice coffee');
  print(counts);
  print(mostCommon(counts));
  final stock = {'tea': 2, 'rice': 1};
  print(restock(stock, {'rice': 4, 'coffee': 3}));
  print(stock);
}

Quick Quiz 1 / 3

var stock = {'tea': 5}; print(stock['coffee']); က ဘာကို print ထုတ်မလဲ။

Quick Quiz 2 / 3

Map<String, int> m = {'a': 1}; m.update('b', (v) => v + 1); print(m); ကို run ရင် ဘာဖြစ်မလဲ။

Quick Quiz 3 / 3

var m = {'z': 1, 'a': 2}; m['b'] = 3; m['z'] = 9; print(m.keys.toList()); က ဘာထွက်မလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Map က key တစ်ခုချင်းစီကို value တစ်ခုနဲ့ တွဲပြီး သိမ်းတာပါ၊ key တူရင် value အသစ်က အစားထိုးပါတယ်။
  • map[key] က V? ပြန်ပြီး key မရှိရင် null ဖြစ်တာမို့ ?? default၊ containsKey သို့မဟုတ် null စစ်ချက်နဲ့ ကိုင်တွယ်ပါ။
  • putIfAbsent က မရှိမှ ထည့်တယ်၊ update က ရှိမှ ပြင်တယ် (ifAbsent မပါရင် မရှိတဲ့ key မှာ error)၊ entries က key နဲ့ value ကို တစ်ပြိုင်နက် ပေးတယ်။
  • ပုံမှန် map က insertion order ကို မှတ်ထားတယ်၊ Map<String, dynamic> က type စစ်ဆေးမှုကို လျှော့ပေးတာမို့ နယ်စပ်မှာပဲ သုံးပါ။

Dart language overview — Dart

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Collections (Maps) | Thuta Learning