57

When creating a Map in scala, I call Map(entities.map{e => e.id -> e}), and I get:

found   : scala.collection.mutable.IndexedSeq[(Int, Entity)]
required: (Int, Entity)

This is because the signature for Map.apply is: def apply[A, B](elems: (A, B)*): CC[A, B], which requires a varargs style argument.

Is there a way to convert the IndexedSeq so that it can be accepted via Map.apply?

dsg
  • 12,924
  • 21
  • 67
  • 111

2 Answers2

104

Try this: Map(entities.map{e => e.id -> e}:_*)

Explicitly typing it as a varargs using :_* seems to work.

dsg
  • 12,924
  • 21
  • 67
  • 111
8

Or this should work too:

entities.map{e => e.id -> e} toMap
Antonin Brettsnajdr
  • 4,073
  • 2
  • 20
  • 14