score:9
Accepted answer
scala> persons groupBy (_.year) mapValues (_ map (_.name))
res9: scala.collection.immutable.Map[Int,scala.collection.immutable.Set[String]] = Map(1984 -> Set(Joyce, Ruth), 1987 -> Set(James, Abigail), 1985 -> Set(Tom, Scott))
score:0
Starting Scala 2.13
, most collections are provided with the groupMap method which is an equivalent of a groupBy
followed by mapValues
:
people.groupMap(_.yearOfBirth)(_.name)
// Map[Int,List[String]] = Map(1985 -> List(Tom, Scott), 1984 -> List(Joyce, Ruth), 1987 -> List(Abigail, James))
// equivalent to people.groupBy(_.yearOfBirth).mapValues(_.map(_.name))
given:
case class Person(name: String, yearOfBirth: Int)
val people = List(Person("Tom", 1985), Person("Abigail", 1987), Person("Joyce", 1984), Person("James", 1987), Person("Scott", 1985), Person("Ruth", 1984))
Source: stackoverflow.com
Related Query
- How to select a object field for map value in Scala using groupby
- How to compose column name using another column's value for withColumn in Scala Spark
- How Scala achieve performance improvement for Map and Set by using different Class based on size?
- Scala - how to get the 'type' for a field using reflection api
- How do I use scala and scalatest to see if a list contains an object with a field matching a specific value
- How to replace value in map for a key based on a condition in scala
- How do I write a query for mongodb using the casbah driver for scala that uses a substring and checks if the field is in a list of supplied values?
- How to create a column of row id in Spark dataframe for each distinct column value using Scala
- Reference field of object using string value scala macro
- Using Scala reflection to check for a method on an object or to find key type on a Map
- For a Scala map of key to a tuple, how to select (project?) a specific item from the tuple?
- How to create a map for (key - image name // value - image-file) in Scala
- scala - how to substitute env value for another variable using regex
- Blank value handle for Map field in scala
- How to map the array key value parameters using SCALA
- How the get the classOf for a scala object type
- how to remove key value from map in scala
- Scala: How can I replace value in Dataframes using scala
- How do I call a Scala Object method using reflection?
- How to groupBy using multiple columns in scala collections
- How map only left value from scala Either?
- How to specify schema for CSV file without using Scala case class?
- How to get min by value only in Scala Map
- How to mock a function within Scala object using Mockito?
- How to add a json object in to a json array using scala play?
- How to check which parameters of case class have default value using scala reflection 2.10
- How to access all public members of an object recursively using Scala reflection?
- how to print variable name and value using a scala macro?
- Scala - How to convert from List of tuples of type (A,B) to type (B,A) using map
- How can I select a non-sequential subset elements from an array using Scala and Spark?
More Query from same tag
- Return a different type according to an input parameter
- How to compile scala into runnable jar file
- Exit code of a jvm application running as a systemd service
- How to apply reduceByKey in Spark on some of fields in a Class?
- SBT run single test class in submodule
- How to remove entries that contain empty values in an RDD that contains csv data?
- Difference between Traits?
- Scala - Implement round robin without mutable types
- Using a SAT solver from Scala class
- Scala manifest and instances
- How correctly pass parameters in controllers?
- Output in Yield statement is different
- Executing side effect code inside scala for-comprehension block
- Would there be a race condition when multiple Actors access/call Object class methods in Scala?
- Accessing the next element in list to compare in Scala
- "Higher Kinded Type Should be Enabled" Warning
- How to randomly sample from a Scala list or array?
- Gatling Performance - flushHttpCache
- Scala - Getting sequence id of elements during map
- How to get javadoc when using sbt and Eclipse?
- Call method Dynamically based on param in Scala
- Scala 2.13: return same collection type (even Array and String)
- How to pass error message dynamically to a custom exception in scala
- Maintain order in .groupBy() operation of Scala collection
- How should I handle the html output generated in my Play 2 app by default helper or twitterBootstrap helper
- Overload constructor with different param type
- Pass json file into JAR or read from spark session
- Using ReduceByKey to group list of values
- Why does "+" operator work even without space between the operands in Scala
- KafkaProducer for both keyed and unkeyed ProducerRecords