Home > Mobile >  Flutter: How to add entry to map if it not contains key from other map
Flutter: How to add entry to map if it not contains key from other map

Time:04-05

I have two maps answers and photos. In my function I have third map witch I filled with photos maps data. And now I want to add answers data to this map as well. Thing is both answers and photos have same keys, but different data. So I need to add only entries with different key. Here is simplified example:

void testFunction() {
  Map<String, String> answers = {
    'key1': 'value1',
    'key2': 'value2',
    'key3': 'value3',
    'key4': 'value4',
    'key5': 'value5',
  };
  Map<String, String> photos = {
    'key3': 'svalue3',
    'key4': 'svalue4',
  };

  Map<String, String> result = {};

  for (final entry in photos.entries) {
    result.putIfAbsent(entry.key, () => entry.value);
  }

  //Here I need to add answers entries to result map if result does not contain same key

  print('Results map data = ${result}');
}

What I want to output is Results map data = {key1: value1, key2: value2, key3: svalue3, key4: svalue4, key5: value5}

CodePudding user response:

Another way to look at it, that seems simpler in my opinion, is that you want to merge the two maps, photos and answers with entries in photos overwriting any entry in answers that have the same key.

In that case you can just do a simple map merge:

Map<String, String> result = {...answers, ...photos};

Your function would look like this.

void testFunction() {
  Map<String, String> answers = {
    'key1': 'value1',
    'key2': 'value2',
    'key3': 'value3',
    'key4': 'value4',
    'key5': 'value5',
  };
  Map<String, String> photos = {
    'key3': 'svalue3',
    'key4': 'svalue4',
  };
 Map<String, String> result = {...answers, ...photos};
  print('Results map data = ${result}');
}

Console output:

Results map data = {key1: value1, key2: value2, key3: svalue3, key4: svalue4, key5: value5}

CodePudding user response:

just do exactly the same for answers after it

for (final entry in answers.entries) {
  result.putIfAbsent(entry.key, () => entry.value);
}

That is what putIfAbsent does, it only puts it if absent, as the name implies

CodePudding user response:

void testFunction() {
  Map<String, String> answers = {
    'key1': 'value1',
    'key2': 'value2',
    'key3': 'value3',
    'key4': 'value4',
    'key5': 'value5',
  };
  Map<String, String> photos = {
    'key3': 'svalue3',
    'key4': 'svalue4',
  };

  Map<String, String> result = {};
  result.addAll(photos);
  for (final entry in answers.entries) {
    result.putIfAbsent(entry.key, () => entry.value);
  }

  //Here i need to add answers entries to result map if result does not contain same key

  print('Results map data = ${result}');
}

UPD: or you can even skip this check if(!photos.containsKey(entry.key))

CodePudding user response:

Map<String, String> result = Map.from(photos);

for (final entry in answers.entries) {
  result.putIfAbsent(entry.key, () => entry.value);
}

CodePudding user response:

for (var map in [answers, photos]) {
   for (var entry in map.entries) {
      result[entry.key] = entry.value;
   }
}

CodePudding user response:

So in your case you want to add the second the second map to the first one and override the entries.

 result = answers;

 photos.forEach((key,value){
    answers[key] = value;
  });

result = {key1: value1, key2: value2, key3: svalue3, key4: svalue4, key5: value5}

  • Related