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

*** ** * ** ***


```
public final class MutableSelection<K> extends Selection<K>
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||
| ↳ | [androidx.recyclerview.selection.Selection](https://developer.android.com/reference/androidx/recyclerview/selection/Selection)\<K\> ||
|   | ↳ | [androidx.recyclerview.selection.MutableSelection](https://developer.android.com/reference/androidx/recyclerview/selection/MutableSelection) |

*** ** * ** ***

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

While the `https://developer.android.com/reference/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/androidx/recyclerview/selection/SelectionTracker#getSelection()` is a live view of the underlying selection, mutable by the library itself.

`https://developer.android.com/reference/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**

```
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/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/androidx/recyclerview/selection/MutableSelection#MutableSelection()()` |

| ### Public methods |
|---|---|
| `boolean` | `https://developer.android.com/reference/androidx/recyclerview/selection/MutableSelection#add(K)(@https://developer.android.com/reference/androidx/annotation/NonNull K key)` Adds a new item to the primary selection. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/selection/MutableSelection#clear()()` Clears the primary selection. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/selection/MutableSelection#copyFrom(androidx.recyclerview.selection.Selection<K>)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/selection/Selection<K> source)` Clones primary and provisional selection from supplied `https://developer.android.com/reference/androidx/recyclerview/selection/Selection`. |
| `boolean` | `https://developer.android.com/reference/androidx/recyclerview/selection/MutableSelection#remove(K)(@https://developer.android.com/reference/androidx/annotation/NonNull K key)` Removes an item from the primary selection. |

### Inherited methods

From [java.lang.Iterable](https://developer.android.com/reference/java/lang/Iterable.html) `void` `https://developer.android.com/reference/java/lang/Iterable.html#forEach(java.util.function.Consumer&lt;? super T&gt;)(https://developer.android.com/reference/java/util/function/Consumer.html<T> action)` `abstract https://developer.android.com/reference/java/util/Iterator.html<T>` `https://developer.android.com/reference/java/lang/Iterable.html#iterator()()` `https://developer.android.com/reference/java/util/Spliterator.html<T>` `https://developer.android.com/reference/java/lang/Iterable.html#spliterator()()` From [androidx.recyclerview.selection.Selection](https://developer.android.com/reference/androidx/recyclerview/selection/Selection) `boolean` `https://developer.android.com/reference/androidx/recyclerview/selection/Selection#contains(K)(@https://developer.android.com/reference/androidx/annotation/Nullable K key)` `boolean` `https://developer.android.com/reference/androidx/recyclerview/selection/Selection#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object.html other)` `int` `https://developer.android.com/reference/androidx/recyclerview/selection/Selection#hashCode()()` `boolean` `https://developer.android.com/reference/androidx/recyclerview/selection/Selection#isEmpty()()` `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Iterator.html<K>` `https://developer.android.com/reference/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.
`int` `https://developer.android.com/reference/androidx/recyclerview/selection/Selection#size()()` `https://developer.android.com/reference/java/lang/String.html` `https://developer.android.com/reference/androidx/recyclerview/selection/Selection#toString()()`

## Public constructors

### MutableSelection

Added in 1.0.0

```
public MutableSelection()
```

## Public methods

### add

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

```
public boolean add(@NonNull K key)
```

Adds a new item to the primary selection.

| Returns |
|---|---|
| `boolean` | 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)

```
public void clear()
```

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)

```
public void copyFrom(@NonNull Selection<K> source)
```

Clones primary and provisional selection from supplied `https://developer.android.com/reference/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)

```
public boolean remove(@NonNull K key)
```

Removes an item from the primary selection.

| Returns |
|---|---|
| `boolean` | true if the operation resulted in a modification to the selection. |