# MapColumn

Artifact: [androidx.room3:room3-common](https://developer.android.com/jetpack/androidx/releases/room3) [View Source](https://cs.android.com/search?q=file:androidx/room3/MapColumn.kt+class:androidx.room3.MapColumn) Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/room3#3.0.0)

*** ** * ** ***

Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

  ```
  @Target(allowedTargets = [AnnotationTarget.TYPE])
  @Retention(value = AnnotationRetention.BINARY)
  annotation MapColumn
  ```

*** ** * ** ***

Declares which column is used to build a map or multimap return value in a `https://developer.android.com/reference/kotlin/androidx/room3/Dao` query function.

This annotation is required when the key or value of a `Map` (or nested map) is a single column of one of the built-in types (primitives, enums, `String`, `ByteArray`) or a type with a `https://developer.android.com/reference/kotlin/androidx/room3/ColumnTypeConverter` (e.g. `Date`, UUID, etc).

The use of this annotation provides clarity on which column should be used in retrieving information required by the DAO function return type.

Example:

```kotlin
@Query("SELECT * FROM Artist JOIN Song ON Artist.artistName = Song.artist")
suspend fun getArtistNameToSongNames():
    Map<@MapColumn("artistName") String, @MapColumn("songName") List<String>>

@Query(
    """
    SELECT *, COUNT(mSongId) as songCount
    FROM Artist JOIN Song ON  Artist.artistName = Song.artist GROUP BY artistName
    """
)
suspend fun getArtistAndSongCounts():
    Map<Artist, @MapColumn(columnName = "songCount") Integer>
```

Column(s) specified in this `@MapColumn`'s annotation values must be present in the query result.

## Summary

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/room3/MapColumn#MapColumn(kotlin.String,kotlin.String)(columnName: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, tableName: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)` | Cmn |

| ### Public properties |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/room3/MapColumn#columnName()` The name of the column to be used for the map's key or value. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/room3/MapColumn#tableName()` The name of the table or alias to be used for the map's column. | Cmn |

## Public constructors

### MapColumn

Cmn Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/room3#3.0.0)

```
MapColumn(columnName: String, tableName: String = "")
```

## Public properties

### columnName

Cmn

```
val columnName: String
```

The name of the column to be used for the map's key or value.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | The column name. |

### tableName

Cmn

```
val tableName: String
```

The name of the table or alias to be used for the map's column.

Providing this value is optional. Useful for disambiguating between duplicate column names. For example, consider the following query: `SELECT * FROM Artist AS a JOIN Song AS s ON a.id == s.artistId`, then the `@MapColumn` usage for a return type `Map<String, List<Song>>` would be `Map<@MapColumn(columnName = "id", tableName = "a") String, List<Song>>`.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | The column table name. |