Count occurrences of each element in a List[List[T]] in Scala

后端 未结 3 925
猫巷女王i
猫巷女王i 2020-12-11 03:07

Suppose you have

val docs = List(List(\"one\", \"two\"), List(\"two\", \"three\"))

where e.g. List(\"one\", \"two\") represents a document

3条回答
  •  情书的邮戳
    2020-12-11 03:52

    Starting Scala 2.13, after flattening the list of lists, we can use groupMapReduce which is a one-pass alternative to groupBy/mapValues:

    // val docs = List(List("one", "two"), List("two", "three"))
    docs.flatten.groupMapReduce(identity)(_ => 1)(_ + _)
    // Map[String,Int] = Map("one" -> 1, "three" -> 1, "two" -> 2)
    

    This:

    • flattens the List of Lists as a List

    • groups list elements (identity) (group part of groupMapReduce)

    • maps each grouped value occurrence to 1 (_ => 1) (map part of groupMapReduce)

    • reduces values within a group of values (_ + _) by summing them (reduce part of groupMapReduce).

提交回复
热议问题