# groupBy

## definition

```scala
groupBy[K](f: (A) ⇒ K): immutable.Map[K, Repr]
```

## demo

```scala
val donuts: Seq[String] = Seq("Plain Donut", "Plain Donut2","Strawberry Donut", "Glazed Donut")

// group elements in a sequence using the groupBy function
val donutsGroup: Map[Char, Seq[String]] = donuts.groupBy(_.charAt(0))
```

按照首字母进行分组,分组的结果需要梳理清楚。

输出结果为

```scala
scala> val donutsGroup: Map[Char, Seq[String]] = donuts.groupBy(_.charAt(0))
donutsGroup: Map[Char,Seq[String]] = Map(S -> List(Strawberry Donut), G -> List(Glazed Donut), P -> List(Plain Donut, Plain Donut2))
```

## demo1

```scala
case class Donut(name: String, price: Double)

val donuts: Seq[Donut] = Seq(Donut("Plain Donut", 1.5), Donut("Strawberry Donut", 2.0), Donut("Glazed Donut", 2.5))

// group case classes donut objects by the name property
val donutsGroup:Map[String,Seq[Donut]] = donuts.groupBy(_.name)
```

输出结果为

```scala
scala> val donutsGroup:Map[String,Seq[Donut]] = donuts.groupBy(_.name)
donutsGroup: Map[String,Seq[Donut]] = Map(Glazed Donut -> List(Donut(Glazed Donut,2.5)), Plain Donut -> List(Donut(Plain Donut,1.5)), Strawberry Donut -> List(Donut(Strawberry Donut,2.0)))
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.selinux.tech/scala/function-operation/g/groupby.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
