# 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)

*** ** * ** ***


```
public final class MutablePreferences extends Preferences
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||
| ↳ | [androidx.datastore.preferences.core.Preferences](https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences) ||
|   | ↳ | [androidx.datastore.preferences.core.MutablePreferences](https://developer.android.com/reference/androidx/datastore/preferences/core/MutablePreferences) |

*** ** * ** ***

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

## Summary

| ### Public methods |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Map.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences.Key<@https://developer.android.com/reference/androidx/annotation/NonNull ?>, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Object.html>` | `https://developer.android.com/reference/androidx/datastore/preferences/core/MutablePreferences#asMap()()` Retrieve a map of all key preference pairs. |
| `final void` | `https://developer.android.com/reference/androidx/datastore/preferences/core/MutablePreferences#clear()()` |
| `boolean` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/datastore/preferences/core/MutablePreferences#contains(androidx.datastore.preferences.core.Preferences.Key)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences.Key<@https://developer.android.com/reference/androidx/annotation/NonNull T> key)` Returns true if this Preferences contains the specified key. |
| `boolean` | `https://developer.android.com/reference/androidx/datastore/preferences/core/MutablePreferences#equals(kotlin.Any)(https://developer.android.com/reference/java/lang/Object.html other)` |
| `T` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/datastore/preferences/core/MutablePreferences#get(androidx.datastore.preferences.core.Preferences.Key)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences.Key<@https://developer.android.com/reference/androidx/annotation/NonNull T> key)` Get a preference with a key. |
| `int` | `https://developer.android.com/reference/androidx/datastore/preferences/core/MutablePreferences#hashCode()()` |
| `final void` | `https://developer.android.com/reference/androidx/datastore/preferences/core/MutablePreferences#minusAssign(androidx.datastore.preferences.core.Preferences.Key)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences.Key<@https://developer.android.com/reference/androidx/annotation/NonNull ?> key)` Removes the preference with the given key from this MutablePreferences. |
| `final void` | `https://developer.android.com/reference/androidx/datastore/preferences/core/MutablePreferences#plusAssign(androidx.datastore.preferences.core.Preferences.Pair)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences.Pair<@https://developer.android.com/reference/androidx/annotation/NonNull ?> pair)` Appends or replaces all `https://developer.android.com/reference/androidx/datastore/preferences/core/MutablePreferences#plusAssign(androidx.datastore.preferences.core.Preferences.Pair)` to this MutablePreferences. |
| `final void` | `https://developer.android.com/reference/androidx/datastore/preferences/core/MutablePreferences#plusAssign(androidx.datastore.preferences.core.Preferences)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences prefs)` Appends or replaces all pairs from `https://developer.android.com/reference/androidx/datastore/preferences/core/MutablePreferences#plusAssign(androidx.datastore.preferences.core.Preferences)` to this MutablePreferences. |
| `final void` | `https://developer.android.com/reference/androidx/datastore/preferences/core/MutablePreferences#putAll(kotlin.Array)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences.Pair<@https://developer.android.com/reference/androidx/annotation/NonNull ?>... pairs)` Appends or replaces all `https://developer.android.com/reference/androidx/datastore/preferences/core/MutablePreferences#putAll(kotlin.Array)` to this MutablePreferences. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull T` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/datastore/preferences/core/MutablePreferences#remove(androidx.datastore.preferences.core.Preferences.Key)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences.Key<@https://developer.android.com/reference/androidx/annotation/NonNull T> key)` Remove a preferences from this MutablePreferences. |
| `final void` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/datastore/preferences/core/MutablePreferences#set(androidx.datastore.preferences.core.Preferences.Key,kotlin.Any)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences.Key<@https://developer.android.com/reference/androidx/annotation/NonNull T> key, @https://developer.android.com/reference/androidx/annotation/NonNull T value)` Set a key value pair in MutablePreferences. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/datastore/preferences/core/MutablePreferences#toString()()` For better debugging. |

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

## Public methods

### asMap

```
public @NonNull Map<@NonNull Preferences.Key<@NonNull ?>, @NonNull Object> asMap()
```

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

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Map.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences.Key<@https://developer.android.com/reference/androidx/annotation/NonNull ?>, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Object.html>` | a map containing all the preferences in this Preferences |

### clear

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

```
public final void clear()
```

### contains

```
public boolean <T extends Object> contains(@NonNull Preferences.Key<@NonNull T> key)
```

Returns true if this Preferences contains the specified key.

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

### equals

```
public boolean equals(Object other)
```

### get

```
public T <T extends Object> get(@NonNull Preferences.Key<@NonNull T> key)
```

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/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 extends https://developer.android.com/reference/java/lang/Object.html>` | the type of the preference |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences.Key<@https://developer.android.com/reference/androidx/annotation/NonNull T> key` | 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/androidx/datastore/preferences/core/Preferences#get(androidx.datastore.preferences.core.Preferences.Key)` but it cannot be cast to T |

### hashCode

```
public int hashCode()
```

### minusAssign

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

```
public final void minusAssign(@NonNull Preferences.Key<@NonNull ?> key)
```

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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences.Key<@https://developer.android.com/reference/androidx/annotation/NonNull ?> key` | the key to remove from this MutablePreferences |

### plusAssign

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

```
public final void plusAssign(@NonNull Preferences.Pair<@NonNull ?> pair)
```

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

Example usage: mutablePrefs += COUNTER_KEY to 100

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

### plusAssign

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

```
public final void plusAssign(@NonNull Preferences prefs)
```

Appends or replaces all pairs from `https://developer.android.com/reference/androidx/datastore/preferences/core/MutablePreferences#plusAssign(androidx.datastore.preferences.core.Preferences)` to this MutablePreferences. Keys in `https://developer.android.com/reference/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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences prefs` | Preferences to append to this MutablePreferences |

### putAll

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

```
public final void putAll(@NonNull Preferences.Pair<@NonNull ?>... pairs)
```

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

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences.Pair<@https://developer.android.com/reference/androidx/annotation/NonNull ?>... pairs` | the pairs to append to this MutablePreferences |

### remove

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

```
public final @NonNull T <T extends Object> remove(@NonNull Preferences.Key<@NonNull T> key)
```

Remove a preferences from this MutablePreferences.

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

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull T` | the original value of this preference key. |

### set

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

```
public final void <T extends Object> set(@NonNull Preferences.Key<@NonNull T> key, @NonNull T value)
```

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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences.Key<@https://developer.android.com/reference/androidx/annotation/NonNull T> key` | the preference to set |
| `@https://developer.android.com/reference/androidx/annotation/NonNull T value` | the value to set the preference to |

### toString

```
public @NonNull String toString()
```

For better debugging.