Scala组按列表 [英] Scala groupBy for a list
本文介绍了Scala组按列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个映射,键为字符串,值为该字符串在列表中出现的次数.我尝试了 groupBy
方法,但没有成功.
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> 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)
这篇关于Scala组按列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文