Scala groupBy for a list - Stack Overflow
stackoverflow.com › questions › 47327360Feb 4, 2019 · Scala groupBy for a list. Ask Question. Asked 5 years, 2 months ago. Modified 5 months ago. Viewed 27k times. 18. I'd like to create a map on which the key is the string and the value is the number of how many times the string appears on the list. I tried the groupBy method, but have been unsuccessful with that. scala.
Scala Tutorial - GroupBy Function Example
allaboutscala.com › scala-groupby-exampleMar 16, 2018 · The groupBy method takes a predicate function as its parameter and uses it to group elements by key and values into a Map collection. As per the Scala documentation, the definition of the groupBy method is as follows: groupBy[K](f: (A) ⇒ K): immutable.Map[K, Repr] The groupBy method is a member of the TraversableLike trait.
Scala groupBy for a list - Stack Overflow
https://stackoverflow.com/questions/47327360scala> val l = List ("abc","abc","cbe","cab") l: List [String] = List (abc, abc, cbe, cab) scala> l.groupBy (identity).mapValues (_.size) res91: scala.collection.immutable.Map [String,Int] = Map (cab -> 1, abc -> 2, cbe -> 1) Share Improve this answer Follow answered Nov 16, 2017 at 10:45 Mahesh Chand 3,110 17 36 identity function in Predef
GroupBy in scala - Stack Overflow
stackoverflow.com › questions › 33472496Nov 2, 2015 · scala> a.groupBy (_._1).mapValues (_.map (_._2)) res2: scala.collection.immutable.Map [Int,List [Int]] = Map (4 -> List (5), 1 -> List (2, 3), 3 -> List (4, 5)) Share Follow answered Nov 2, 2015 at 7:24 Shadowlands 15k 4 44 43 3 Note that mapValues is lazy, so _.map (_._2) will be called each time you access a value from the resulting Map.