0

I have two list of objects like so :

class KeyValue {
  KeyValue({required this.key, required this.value});

  final String key;
  final String value;
}

class CustomClass {
  CustomClass({
    required this.key,
    required this.name,
    this.value,
  });

  final String key;
  final String name;
  final String? value;
}

var listCustomClass = [
  CustomClass(key: "1234", name: "name1"),
  CustomClass(key: "1234", name: "name2"),
  CustomClass(key: "12345", name: "name3"),
  CustomClass(key: "12345678", name: "name4"),
];

final listKeyValue = [
  KeyValue(key: "1234", value: "someValue1"),
  KeyValue(key: "12345", value: "someValue2"),
  KeyValue(key: "12345678", value: "someValue3"),
  KeyValue(key: "123456789", value: "someValue4"),
];

I'd like to map listCustomClass & listKeyValue by key in order to create create a new List<CustomClass> with each CustomClass() taking its value parameter from its matching KeyValue() object (the one with the same key)

it would create newListCustomClass like so :


newListCustomClass = [
  CustomClass(key: "1234", name: "name1", value: "someValue1"),
  CustomClass(key: "1234", name: "name2", value: "someValue1"),
  CustomClass(key: "12345", name: "name3", value: "someValue2"),
  CustomClass(key: "12345678", name: "name4", value: "someValue3"),
];

2 Answers 2

2

If listCustomClass or listKeyValue are large, you would be better off converting one of them to a Map first. Since listCustomClass apparently can have elements with the same key, that one should stay a List.

  var mapKeyValue = {
    for (var keyValue in listKeyValue) keyValue.key: keyValue.value,
  };

  var newListCustomClass = [
    for (var customClass in listCustomClass)
      CustomClass(
        key: customClass.key,
        name: customClass.name,
        value: mapKeyValue[customClass.key],
      ),
  ];

If listCustomClass has m elements and listKeyValues has n elements, then the above code would be O(m + n). If you store listKeyValues as a Map in the first place, then the code to convert from List to Map could be removed, and it would be just O(m).

Sign up to request clarification or add additional context in comments.

2 Comments

hi thanks for your answer 🙏truth is my list is quite small & I actually have two values for each KeyValue(key, value1, value2) so converting them to Map won't work - but I will up vote : )
@Sunshine Converting to a Map would still work with multiple values; you'd just need the Map value to be a List of values or a KeyValue object itself.
1

You can do it like

  String getValue(String key) =>
      listKeyValue.firstWhere((element) => element.key == key).value;

  for (int i = 0; i < listCustomClass.length; i++) {
    listCustomClass[i] =
        listCustomClass[i].copyWith(value: getValue(listCustomClass[i].key));
  }

And the class

class CustomClass {
  CustomClass({
    required this.key,
    required this.name,
    this.value,
  });

  final String key;
  final String name;
  final String? value;

  @override
  String toString() => 'CustomClass(key: $key, name: $name, value: $value)';

  CustomClass copyWith({
    String? key,
    String? name,
    String? value,
  }) {
    return CustomClass(
      key: key ?? this.key,
      name: name ?? this.name,
      value: value ?? this.value,
    );
  }
}

3 Comments

Thank you 🙏 do I have to keep listCustomClass as var for it to be modified with the new values from your for loop or can I make it final ? 🙂
Yes listCustomClass can be final, we are modifying elements here
Note that this is O(m * n) with respect to the lengths of listCustomClass and listKeyValue. That should be okay if the lists are small, but it will become inefficient if they're large.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.