The entries of maps are just tuples, knowing that you can just map over your Seq[Car]
and create those tuples. After that just call toMap
and you will get a Map
:
cars.map { car =>
(car.name, car.year) -> car
}.toMap
edit:
If you want to keep duplicates, it is easier to use groupBy
:
cars.groupBy(car => (car.name, car.year))