180

If I have a collection c of type T and there is a property p on T (of type P, say), what is the best way to do a map-by-extracting-key?

val c: Collection[T]
val m: Map[P, T]

One way is the following:

m = new HashMap[P, T]
c foreach { t => m add (t.getP, t) }

But now I need a mutable map. Is there a better way of doing this so that it's in 1 line and I end up with an immutable 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)

13 Answers 13

258

You can use

c map (t => t.getP -> t) toMap

but be aware that this needs 2 traversals.

10
  • 8
    I still prefer my suggestions in trac of a Traversable[K].mapTo( K => V) and Traversable[V].mapBy( V => K) were better! Jul 14, 2010 at 21:17
  • 7
    Be aware that this is a quadratic operation, but the same goes for most other variants given here. Looking at the source code of scala.collection.mutable.MapBuilder etc, it seems to me that for each tuple, a new immutable map is created to which the tuple is added. Mar 3, 2012 at 2:17
  • 32
    On my machine for a list with 500,000 elements, this Scala code is about 20 times slower than the straight-forward Java approach (create HashMap with appropriate size, loop over list, put elements into map). For 5,000 elements, Scala ist about 8 times slower. The loop approach written in Scala is roughly 3 times faster than the toMap variant, but still between 2 and 7 times slower than Java. Mar 11, 2012 at 2:13
  • 8
    Would you please provide the test sources to the SO community? Thx.
    – user573215
    Sep 23, 2013 at 9:11
  • 8
    Replace c with c.iterator to avoid creation of intermediate collection.
    – ghik
    Aug 16, 2014 at 11:14
24

You can construct a Map with a variable number of tuples. So use the map method on the collection to convert it into a collection of tuples and then use the : _* trick to convert the result into a variable argument.

scala> val list = List("this", "maps", "string", "to", "length") map {s => (s, s.length)}
list: List[(java.lang.String, Int)] = List((this,4), (maps,4), (string,6), (to,2), (length,6))

scala> val list = List("this", "is", "a", "bunch", "of", "strings")
list: List[java.lang.String] = List(this, is, a, bunch, of, strings)

scala> val string2Length = Map(list map {s => (s, s.length)} : _*)
string2Length: scala.collection.immutable.Map[java.lang.String,Int] = Map(strings -> 7, of -> 2, bunch -> 5, a -> 1, is -> 2, this -> 4)
3
  • 6
    I've been reading about Scala for >2 weeks and working through examples and not once had I seen this ": _ *" notation! Thanks very much for your help Mar 23, 2009 at 21:15
  • Just for the record, i wonder why we need to precise that this is a sequence with _. map still convert return a list of tuple here. So why the _ ? I mean it works but i would like to understand the type ascription here
    – MaatDeamon
    Jul 9, 2015 at 20:18
  • 1
    Is this more efficient than the other methods?
    – Jus12
    Aug 22, 2015 at 8:14
20

In addition to @James Iry's solution, it is also possible to accomplish this using a fold. I suspect that this solution is slightly faster than the tuple method (fewer garbage objects are created):

val list = List("this", "maps", "string", "to", "length")
val map = list.foldLeft(Map[String, Int]()) { (m, s) => m(s) = s.length }
9
  • I will try this out (I'm sure it works :-). What is going on with the "(m,s)=>m(s) = s.length" function? I have seen the typical foldLeft example with a sum and a function "_ + _"; this is much more confusing! The function seems to assume that I already have a tuple (m,s), which I don't really get Mar 24, 2009 at 22:33
  • 2
    Man, Scala was weird back then! Feb 4, 2012 at 9:53
  • 9
    @Daniel I try your code, but appear following error: "value update is not a member of scala.collection.immutable.Map[String,Int]". Please explain your code how to working this code?
    – SBotirov
    Feb 18, 2014 at 15:15
  • 1
    doesnt seem to work.for me either "Application does not take parameters"
    – jayunit100
    Feb 15, 2015 at 22:05
  • 7
    Immutable version: list.foldLeft(Map[String,Int]()) { (m,s) => m + (s -> s.length) }. Note that if you want to use comma to build the tuple, you need an extra pair of parentheses: ((s, s.length)).
    – Kelvin
    Apr 18, 2017 at 23:04
14

This can be implemented immutably and with a single traversal by folding through the collection as follows.

val map = c.foldLeft(Map[P, T]()) { (m, t) => m + (t.getP -> t) }

The solution works because adding to an immutable Map returns a new immutable Map with the additional entry and this value serves as the accumulator through the fold operation.

The tradeoff here is the simplicity of the code versus its efficiency. So, for large collections, this approach may be more suitable than using 2 traversal implementations such as applying map and toMap.

9

Another solution (might not work for all types)

import scala.collection.breakOut
val m:Map[P, T] = c.map(t => (t.getP, t))(breakOut)

this avoids the creation of the intermediary list, more info here: Scala 2.8 breakOut

8

What you're trying to achieve is a bit undefined.
What if two or more items in c share the same p? Which item will be mapped to that p in the map?

The more accurate way of looking at this is yielding a map between p and all c items that have it:

val m: Map[P, Collection[T]]

This could be easily achieved with groupBy:

val m: Map[P, Collection[T]] = c.groupBy(t => t.p)

If you still want the original map, you can, for instance, map p to the first t that has it:

val m: Map[P, T] = c.groupBy(t => t.p) map { case (p, ts) =>  p -> ts.head }
3
  • 1
    One handy tweak on this is to use collect instead of map. Eg: c.group(t => t.p) collect { case (Some(p), ts) => p -> ts.head }. This way you can do things like flatten maps when you key is an Option[_].
    – healsjnr
    Feb 16, 2016 at 4:54
  • @healsjnr Sure, this could be said for any map. It isn't the core issue here, though.
    – Eyal Roth
    Feb 16, 2016 at 10:39
  • 1
    You could use .mapValues(_.head) instead of the map.
    – lex82
    Apr 3, 2018 at 15:37
6

Scala 2.13+

instead of "breakOut" you could use

c.map(t => (t.getP, t)).to(Map)

Scroll to "View": https://www.scala-lang.org/blog/2017/02/28/collections-rework.html

2

This is probably not the most efficient way to turn a list to map, but it makes the calling code more readable. I used implicit conversions to add a mapBy method to List:

implicit def list2ListWithMapBy[T](list: List[T]): ListWithMapBy[T] = {
  new ListWithMapBy(list)
}

class ListWithMapBy[V](list: List[V]){
  def mapBy[K](keyFunc: V => K) = {
    list.map(a => keyFunc(a) -> a).toMap
  }
}

Calling code example:

val list = List("A", "AA", "AAA")
list.mapBy(_.length)                  //Map(1 -> A, 2 -> AA, 3 -> AAA)

Note that because of the implicit conversion, the caller code needs to import scala's implicitConversions.

0
2
c map (_.getP) zip c

Works well and is very intuitiv

4
  • 9
    Please add more details. Dec 4, 2014 at 10:57
  • 2
    I'm sorry. But, this IS an answer to the question "Scala best way of turning a Collection into a Map-by-key?" as Ben Lings is. Dec 15, 2014 at 15:55
  • 1
    And Ben didn't provide any explanation?
    – shinzou
    Nov 7, 2017 at 12:41
  • 1
    this creates two lists and combine into a "map" using the elements in c as key (sort of). Note "map" because the resulting collection is not a scala Map but creates another list/iterable of tuples...but the effect is the same for the OP's purpose i wouldn't discount the simplicity but it's not as efficient as foldLeft solution, nor it's the real answer to the question "converting into a collection into a map-by-key" Feb 11, 2018 at 14:14
2

How about using zip and toMap?

myList.zip(myList.map(_.length)).toMap
1

For what it's worth, here are two pointless ways of doing it:

scala> case class Foo(bar: Int)
defined class Foo

scala> import scalaz._, Scalaz._
import scalaz._
import Scalaz._

scala> val c = Vector(Foo(9), Foo(11))
c: scala.collection.immutable.Vector[Foo] = Vector(Foo(9), Foo(11))

scala> c.map(((_: Foo).bar) &&& identity).toMap
res30: scala.collection.immutable.Map[Int,Foo] = Map(9 -> Foo(9), 11 -> Foo(11))

scala> c.map(((_: Foo).bar) >>= (Pair.apply[Int, Foo] _).curried).toMap
res31: scala.collection.immutable.Map[Int,Foo] = Map(9 -> Foo(9), 11 -> Foo(11))
1
  • Also, fwiw, this is how those two would look in Haskell: Map.fromList $ map (bar &&& id) c, Map.fromList $ map (bar >>= (,)) c. Feb 4, 2012 at 10:08
-2

This works for me:

val personsMap = persons.foldLeft(scala.collection.mutable.Map[Int, PersonDTO]()) {
    (m, p) => m(p.id) = p; m
}

The Map has to be mutable and the Map has to be return since adding to a mutable Map does not return a map.

1
  • 1
    Actually, it can be implemented immutably as follows: val personsMap = persons.foldLeft(Map[Int, PersonDTO]()) { (m, p) => m + (p.id -> p) } The Map can be immutable, as evidenced above, because adding to an immutable Map returns a new immutable Map with the additional entry. This value serves as the accumulator through the fold operation.
    – RamV13
    Dec 13, 2016 at 17:43
-3

use map() on collection followed with toMap

val map = list.map(e => (e, e.length)).toMap
1
  • 4
    How is this different from the answer that was submitted, and accepted, 7 years ago?
    – jwvh
    Dec 24, 2017 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.