# MutableSelection

Artifact: [androidx.recyclerview:recyclerview-selection](https://developer.android.com/jetpack/androidx/releases/recyclerview) [View Source](https://cs.android.com/search?q=file:androidx/recyclerview/selection/MutableSelection.java+class:androidx.recyclerview.selection.MutableSelection) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

*** ** * ** ***


```
class MutableSelection<K> : Selection<K!>
```

<br />

|---|---|---|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) |||
| ↳ | [androidx.recyclerview.selection.Selection](https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/Selection)\<K!\> ||
|   | ↳ | [androidx.recyclerview.selection.MutableSelection](https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/MutableSelection) |

*** ** * ** ***

Subclass of `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/Selection` exposing public support for mutating the underlying selection data. This is useful for clients of `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/SelectionTracker` that wish to manipulate a copy of selection data obtained via `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/SelectionTracker#copySelection(androidx.recyclerview.selection.MutableSelection<K>)`.

While the `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/Selection` class is not intrinsically immutable, it is not mutable by non-library code. Furthermore the value returned from `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/SelectionTracker#getSelection()` is a live view of the underlying selection, mutable by the library itself.

`https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/MutableSelection` allows clients to obtain a mutable copy of the Selection state held by the selection library. This is useful in situations where a stable snapshot of the selection is required.

**Example**

```kotlin
MutableSelection snapshot = new MutableSelection();
selectionTracker.copySelection(snapshot);

// Clear the user visible selection.
selectionTracker.clearSelection();
// tracker.getSelection().isEmpty() will be true.
// shapshot has a copy of the previous selection.
```

| Parameters |
|---|---|
| `<K>` | Selection key type. @see `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/StorageStrategy` for supported types. |

| See also |
|---|---|
| `https://developer.android.com/reference/android/text/Selection.html` |   |

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/MutableSelection#MutableSelection()()` |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/MutableSelection#add(K)(key: K)` Adds a new item to the primary selection. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/MutableSelection#clear()()` Clears the primary selection. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/MutableSelection#copyFrom(androidx.recyclerview.selection.Selection<K>)(source: https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/Selection<K!>)` Clones primary and provisional selection from supplied `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/Selection`. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/MutableSelection#remove(K)(key: K)` Removes an item from the primary selection. |

### Inherited functions

From [java.lang.Iterable](https://developer.android.com/reference/java/lang/Iterable.html) `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-iterable/for-each.html(action: https://developer.android.com/reference/java/util/function/Consumer.html<T!>!)` `abstract (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-iterator/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-iterator/index.html<T!>!` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-iterable/iterator.html()` `https://developer.android.com/reference/java/util/Spliterator.html<T!>!` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-iterable/spliterator.html()` From [androidx.recyclerview.selection.Selection](https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/Selection) `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/Selection#contains(K)(key: K?)` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/Selection#equals(java.lang.Object)(other: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html!)` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/Selection#hashCode()()` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/Selection#isEmpty()()` `(https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-iterator/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-iterator/index.html<K!>` `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/Selection#iterator()()`

Returns an `https://developer.android.com/reference/java/util/Iterator.html` that iterators over the selection, \*excluding\* any provisional selection.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/Selection#size()()` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/Selection#toString()()`

## Public constructors

### MutableSelection

Added in 1.0.0

```
MutableSelection()
```

## Public functions

### add

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

```
fun add(key: K): Boolean
```

Adds a new item to the primary selection.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | true if the operation resulted in a modification to the selection. |

### clear

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

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

Clears the primary selection. The provisional selection, if any, is unaffected.

### copyFrom

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

```
fun copyFrom(source: Selection<K!>): Unit
```

Clones primary and provisional selection from supplied `https://developer.android.com/reference/kotlin/androidx/recyclerview/selection/Selection`. Does not copy active range data.

### remove

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

```
fun remove(key: K): Boolean
```

Removes an item from the primary selection.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | true if the operation resulted in a modification to the selection. |