groupMap
方法(或屬性)屬於 scala.collection.View.ZipWithIndex
類(class),其相關用法說明如下。
用法:
def groupMap[K, B](key: ((A, Int)) => K)(f: ((A, Int)) => B): Map[K, View[B]]
根據鑒別函數key
將此可迭代集合劃分為可迭代集合的映射.使用value
函數將組中的每個元素轉換為B
類型的值。
它等效於 groupBy(key).mapValues(_.map(f))
,但效率更高。
case class User(name: String, age: Int)
def namesByAge(users: Seq[User]): Map[Int, Seq[String]] =
users.groupMap(_.age)(_.name)
注意:即使應用於視圖或惰性集合,它也會始終強製元素。
類型參數:
- B
轉換函數返回的值的類型
- K
鑒別器函數返回的鍵的類型
值參數:
- f
元素變換函數
- key
判別器函數
繼承自:
- collection.IterableOps.groupMap
源碼:
- Iterable.scala
相關用法
- Scala ZipWithIndex.groupMapReduce用法及代碼示例
- Scala ZipWithIndex.groupBy用法及代碼示例
- Scala ZipWithIndex.inits用法及代碼示例
- Scala ZipWithIndex.partitionMap用法及代碼示例
- Scala ZipWithIndex.scanRight用法及代碼示例
- Scala ZipWithIndex.transpose用法及代碼示例
- Scala ZipWithIndex.tails用法及代碼示例
- Scala ZipWithIndex.sliding用法及代碼示例
- Scala ZipWithIndex.sizeIs用法及代碼示例
- Scala ZipWithIndex.lazyZip用法及代碼示例
- Scala ZipWithIndex.sizeCompare用法及代碼示例
- Scala ZipWithIndex.unzip3用法及代碼示例
- Scala ZipWithIndex.collectFirst用法及代碼示例
- Scala ZipWithIndex.addString用法及代碼示例
- Scala ZipWithIndex.mkString用法及代碼示例
- Scala ZipWithIndex.unzip用法及代碼示例
- Scala ZippedIterable2.unzip用法及代碼示例
- Scala ZippedIterable2.addString用法及代碼示例
- Scala ZipAll.groupBy用法及代碼示例
- Scala ZippedIterable3.transpose用法及代碼示例
- Scala ZipAll.unzip用法及代碼示例
- Scala ZippedIterable3.inits用法及代碼示例
- Scala Zip.transpose用法及代碼示例
- Scala ZipAll.addString用法及代碼示例
- Scala ZipAll.inits用法及代碼示例
注:本文由純淨天空篩選整理自scala-lang.org大神的英文原創作品 ZipWithIndex.groupMap。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。