Reputation: 2435
I am new to Scala so I am a bit fighting with maps.
I have
val items = Seq[MyModel]
where MyModel (came from Java) contains getLang, getName and getMessage methods.
Now I need to fill up the
var loadedMessagesMap: Map[String, Map[String, String]] = ListMap.empty
to contain values grouped by lang in structure: lang -> (name -> message). Name property is unique. Thank you.
Upvotes: 0
Views: 706
Reputation: 7591
Maybe this will help you:
val result: Map[String, Map[String, Seq[String]]] = items.groupBy(_.getLang).map {
case(lang, models) =>
lang -> models.groupBy(_.getName).mapValues(_.map(_.getMessage))
}
It returns a Seq[String]
because there might be several messages for the same language and name. Not sure how you want to handle that case.
Upvotes: 2
Reputation: 2480
This should do the trick:
val models: Seq[MyModel] = ???
val mapped = models.map { model =>
model.getLang -> Map(model.getName -> model.getMessage)
}.toMap
I hope this helps you.
Upvotes: 0