あなたはそれを自分で行うと、それがどのように動作するか学びたいのであれば、あなたはこれを試すことができます。
scala> case class Person(name: String, surname: String, age:Int)
defined class Person
scala> val people = List(Person("Alice", "Wonderland", 42), Person("Bob", "Father", 42), Person("Bob", "Son", 20))
people: List[Person] = List(Person(Alice,Wonderland,42), Person(Bob,Father,42), Person(Bob,Son,20))
scala> val names = people.map(_.name).distinct
names: List[String] = List(Alice, Bob)
scala> names.map(n => n -> people.filter(_.name == n)).toMap
res0: scala.collection.immutable.Map[String,List[Person]] = Map(Alice -> List(Person(Alice,Wonderland,42)), Bob -> List(Person(Bob,Father,42), Person(Bob,Son,20)))
乾杯
それを行うのは本当に遅い方法です。 O(N^2)を、第2の 'map'のために使用します。 –
@TheArchetypalPaulは、パフォーマンスを考慮していませんでした。操作を助けてくれる情報を操作する代わりの方法です。 groupByは間違いなく簡潔で効率的です。乾杯 – saza