# MutablePreferences

Artifact: [androidx.datastore:datastore-preferences-core](https://developer.android.com/jetpack/androidx/releases/datastore) [View Source](https://cs.android.com/search?q=file:androidx/datastore/preferences/core/Preferences.kt+class:androidx.datastore.preferences.core.MutablePreferences) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/datastore#1.0.0)

*** ** * ** ***

Kotlin \|[Java](https://developer.android.com/reference/androidx/datastore/preferences/core/MutablePreferences "View this page in Java") Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

  ```
  class MutablePreferences : Preferences
  ```

|---|---|---|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) |||
| ↳ | [androidx.datastore.preferences.core.Preferences](https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences) ||
|   | ↳ | [androidx.datastore.preferences.core.MutablePreferences](https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences) |

*** ** * ** ***

Mutable version of `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences`. Allows for creating Preferences with different key-value pairs.

## Summary

| ### Public functions |
|---|---|---|
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-map/index.html<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Key<*>, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html>` | `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences#asMap()()` Retrieve a map of all key preference pairs. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences#clear()()` | Cmn |
| `open operator https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences#contains(androidx.datastore.preferences.core.Preferences.Key)(key: https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Key<T>)` Returns true if this Preferences contains the specified key. | Cmn |
| `open operator https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences#equals(kotlin.Any)(other: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?)` | Cmn |
| `open operator T?` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences#get(androidx.datastore.preferences.core.Preferences.Key)(key: https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Key<T>)` Get a preference with a key. | Cmn |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences#hashCode()()` | Cmn |
| `operator https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences#minusAssign(androidx.datastore.preferences.core.Preferences.Key)(key: https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Key<*>)` Removes the preference with the given key from this MutablePreferences. | Cmn |
| `operator https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences#plusAssign(androidx.datastore.preferences.core.Preferences.Pair)(pair: https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Pair<*>)` Appends or replaces all `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences#plusAssign(androidx.datastore.preferences.core.Preferences.Pair)` to this MutablePreferences. | Cmn |
| `operator https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences#plusAssign(androidx.datastore.preferences.core.Preferences)(prefs: https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences)` Appends or replaces all pairs from `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences#plusAssign(androidx.datastore.preferences.core.Preferences)` to this MutablePreferences. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences#putAll(kotlin.Array)(vararg pairs: https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Pair<*>)` Appends or replaces all `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences#putAll(kotlin.Array)` to this MutablePreferences. | Cmn |
| `T` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences#remove(androidx.datastore.preferences.core.Preferences.Key)(key: https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Key<T>)` Remove a preferences from this MutablePreferences. | Cmn |
| `operator https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences#set(androidx.datastore.preferences.core.Preferences.Key,kotlin.Any)(key: https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Key<T>, value: T)` Set a key value pair in MutablePreferences. | Cmn |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences#toString()()` For better debugging. | Cmn |

| ### Inherited functions |
|---|
| From [androidx.datastore.preferences.core.Preferences](https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences) |---|---|---| | `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences` | `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences#copy(kotlin.Function1)(block: (https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)` Creates a new read-only Preferences with the changes from `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences#copy(kotlin.Function1)`. | Cmn | | `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences` | `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences#toMutablePreferences()()` Gets a mutable copy of Preferences which contains all the preferences in this Preferences. | Cmn | | `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences` | `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences#toPreferences()()` Gets a read-only copy of Preferences which contains all the preferences in this Preferences. | Cmn | |

## Public functions

### asMap

Cmn

```
open fun asMap(): Map<Preferences.Key<*>, Any>
```

Retrieve a map of all key preference pairs. The returned map is unmodifiable, and attempts to mutate it will throw runtime exceptions.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-map/index.html<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Key<*>, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html>` | a map containing all the preferences in this Preferences |

### clear

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

```
fun clear(): Unit
```

### contains

Cmn

```
open operator fun <T : Any?> contains(key: Preferences.Key<T>): Boolean
```

Returns true if this Preferences contains the specified key.

| Parameters |
|---|---|
| `key: https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Key<T>` | the key to check for |

### equals

Cmn

```
open operator fun equals(other: Any?): Boolean
```

### get

Cmn

```
open operator fun <T : Any?> get(key: Preferences.Key<T>): T?
```

Get a preference with a key. If the key is not set, returns null.

If T is Set, this returns an unmodifiable set which will throw a runtime exception when mutated. Do not try to mutate the returned set.

Use `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences#set(androidx.datastore.preferences.core.Preferences.Key,kotlin.Any)` to change the value of a preference (inside a DataStore
.edit block).

<br />

| Parameters |
|---|---|
| `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?>` | the type of the preference |
| `key: https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Key<T>` | the key for the preference |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-class-cast-exception/index.html` | if there is something stored with the same name as `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences#get(androidx.datastore.preferences.core.Preferences.Key)` but it cannot be cast to T |

### hashCode

Cmn

```
open fun hashCode(): Int
```

### minusAssign

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

```
operator fun minusAssign(key: Preferences.Key<*>): Unit
```

Removes the preference with the given key from this MutablePreferences. If this Preferences does not contain the key, this is a no-op.

Example usage: mutablePrefs -= COUNTER_KEY

| Parameters |
|---|---|
| `key: https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Key<*>` | the key to remove from this MutablePreferences |

### plusAssign

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

```
operator fun plusAssign(pair: Preferences.Pair<*>): Unit
```

Appends or replaces all `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences#plusAssign(androidx.datastore.preferences.core.Preferences.Pair)` to this MutablePreferences.

Example usage: mutablePrefs += COUNTER_KEY to 100

| Parameters |
|---|---|
| `pair: https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Pair<*>` | the Preference.Pair to add to this MutablePreferences |

### plusAssign

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

```
operator fun plusAssign(prefs: Preferences): Unit
```

Appends or replaces all pairs from `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences#plusAssign(androidx.datastore.preferences.core.Preferences)` to this MutablePreferences. Keys in `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences#plusAssign(androidx.datastore.preferences.core.Preferences)` will overwrite keys in this Preferences.

Example usage: mutablePrefs += preferencesOf(COUNTER_KEY to 100, NAME to "abcdef")

| Parameters |
|---|---|
| `prefs: https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences` | Preferences to append to this MutablePreferences |

### putAll

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

```
fun putAll(vararg pairs: Preferences.Pair<*>): Unit
```

Appends or replaces all `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences#putAll(kotlin.Array)` to this MutablePreferences.

| Parameters |
|---|---|
| `vararg pairs: https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Pair<*>` | the pairs to append to this MutablePreferences |

### remove

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

```
fun <T : Any?> remove(key: Preferences.Key<T>): T
```

Remove a preferences from this MutablePreferences.

| Parameters |
|---|---|
| `key: https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Key<T>` | the key to remove this MutablePreferences |

| Returns |
|---|---|
| `T` | the original value of this preference key. |

### set

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

```
operator fun <T : Any?> set(key: Preferences.Key<T>, value: T): Unit
```

Set a key value pair in MutablePreferences.

Example usage: val COUNTER_KEY = intPreferencesKey("counter")

// Once edit completes successfully, preferenceStore will contain the incremented counter. preferenceStore.edit { prefs: MutablePreferences -\> prefs\\\[COUNTER_KEY\\\] = prefs\\\[COUNTER_KEY\\\] :? 0 + 1 }

| Parameters |
|---|---|
| `key: https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Key<T>` | the preference to set |
| `value: T` | the value to set the preference to |

### toString

Cmn

```
open fun toString(): String
```

For better debugging.