title: Scala best way of turning a Collection into a Map-by-key?
body: <p>If I have a collection <code>c</code> of type <code>T</code> and there is a property <code>p</code> on <code>T</code> (of type <code>P</code>, say), what is the best way to do a <em>map-by-extracting-key</em>?</p>
  
  <pre><code>val c: Collection[T]
  val m: Map[P, T]
  </code></pre>
  
  <p>One way is the following:</p>
  
  <pre><code>m = new HashMap[P, T]
  c foreach { t =&gt; m add (t.getP, t) }
  </code></pre>
  
  <p>But now I need a <em>mutable</em> map. Is there a better way of doing this so that it's in 1 line and I end up with an <em>immutable</em> Map? (Obviously I could turn the above into a simple library utility, as I would in Java, but I suspect that in Scala there is no need)</p>