What is the quickest & most efficient way (memory efficiency) to filter a list of objects into a map with each unique object property being a key with a list of objects with matching object property?
I can’t figure out how to do this without using lots of memory and duplicating the main list. In practice this initial list could have thousands of entries.
void main() {
List<Car> cars = [
Car('BMW', 5),
Car('Mercedes', 5),
Car('Mercedes', 4),
Car('Mercedes', 2),
Car('Ferarri', 2),
Car('Ferarri', 4),
Car('BMW', 6),
Car('BMW', 2),
Car('BMW', 4),
Car('McLaren', 3),
];
Map<String, List<Car>> organizedCars = {};
for (Car car in cars) {
???
}
}
class Car {
String name;
int passangers;
Car(this.name, this.passangers);
}
Required output:
Map<String, List<Car>> organizedCars = {
'BMW': [
Car('BMW', 6),
Car('BMW', 5),
Car('BMW', 2),
Car('BMW', 4),
],
'Mercedes': [
Car('Mercedes', 5),
Car('Mercedes', 4),
Car('Mercedes', 2),
],
etc...
};
2
Answers
You can use
fold
method to group the items together.Here is one impelementation:
Output:
Output is formatted as it was single line in console it would not look pretty here.
For
Map
s that don’t allow nullable values, I prefer using(map[key] ??= []).add(...)
, which I think is more elegant and more compact than usingMap.update(..., ifAbsent: ...)
. It looks up theList
associated with theMap
key, initializing theMap
value to an emptyList
if the lookup fails, and then unconditionally appends an element to the foundList
.Which will print (after some reformatting):