Scala list groupBy() operation with examples

 1,579 total views,  5 views today

Welcome to Scala series. In this tutorial, you will learn about the Scala List groupBy() operation with examples.

Scala groupBy() operation allows us to group the elements into a key and value pairs based on the given function.groupBy() operation will return a Map collection as the output having keys and values.

Scala groupBy() operation is applicable to all types of Scala collection.

groupBy() Syntax

def groupBy[K](f: (A) => K): Map[K, List[A]]

Partitions this iterable collection into a map of iterable collections according to some discriminator function.

Scala List groupBy() examples

In this groupBy() example, we have created a list of Strings and have applied groupBy() to group elements based on the length of each element.

object GroupByTest {

  def main(args: Array[String]): Unit = {

    //create a List
    var list = List("apple", "hello", "London")

    //Apply groupBy
    list.groupBy(ele => ele.length()).foreach(println)

  }
}

Output

(5,List(apple, hello))
(6,List(London))

In another groupBy() example, we are grouping elements based on the first character of each element.

object GroupByTest {

  def main(args: Array[String]): Unit = {

    //create a List
    var list = List("apple", "hello", "London", "Love", "Happy", "harry")

    //Apply groupBy
    list.groupBy(ele => ele.charAt(0)).foreach(println)

  }
}

Output

(h,List(hello, harry))
(L,List(London, Love))
(a,List(apple))
(H,List(Happy))

Let us see another groupBy example on Scala Objects. I have a person objects which has two fields (name and age). We are applying groupBy() to group person based on age.

object GroupByTest {

  def main(args: Array[String]): Unit = {

    case class Person(var name: String, age: Int)

    //create a List
    var list = List(Person("Raj", 27), Person("John", 27), Person("Sri", 26), Person("Radha", 26), Person("Wick", 28))

    //Apply groupBy
    list.groupBy(ele => ele.age).foreach(println)

  }
}

Output

(26,List(Person(Sri,26), Person(Radha,26)))
(28,List(Person(Wick,28)))
(27,List(Person(Raj,27), Person(John,27)))

ReferencesScala groupBy() operation

Would you like to learn more about Scala features, please check out Scala – a complete guide series. In this tutorial guide, you will find useful Scala functions that can be applied to the collection.

I hope you like this tutorial and you were able to understand the Scala list groupBy() operation with examples.

Thanks for reading and please give us a thumb up and comment below.

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.