# Selection

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/Selection.java+class:androidx.recyclerview.selection.Selection) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

*** ** * ** ***


```
public class Selection<K> implements Iterable<K>
```

<br />

Known direct subclasses [MutableSelection](https://developer.android.com/reference/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. |

*** ** * ** ***

Object representing a "primary" selection and a "provisional" selection.

This class tracks selected items by managing two sets:

**Primary Selection**

Primary selection consists of items selected by a user. This represents the selection "at rest", as the selection does not contains items that are in a "provisional" selected state created by way of an ongoing gesture or band operation.

**Provisional Selection**

Provisional selections are selections which are interim in nature.

Provisional selection exists to address issues where a transitory selection might momentarily intersect with a previously established selection resulting in a some or all of the established selection being erased. Such situations may arise when band selection is being performed in "additive" mode (e.g. SHIFT or CTRL is pressed on the keyboard prior to mouse down), or when there's an active gesture selection (which can be initiated by long pressing an unselected item while there is an existing 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/androidx/recyclerview/selection/MutableSelection` |   |

## Summary

| ### Public methods |
|---|---|
| `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()()` |

### 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)` `https://developer.android.com/reference/java/util/Spliterator.html<T>` `https://developer.android.com/reference/java/lang/Iterable.html#spliterator()()`

## Public methods

### contains

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

```
public boolean contains(@Nullable K key)
```

| Returns |
|---|---|
| `boolean` | true if the position is currently selected. |

### equals

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

### hashCode

```
public int hashCode()
```

### isEmpty

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

```
public boolean isEmpty()
```

| Returns |
|---|---|
| `boolean` | true if the selection is empty. |

### iterator

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

```
public @NonNull Iterator<K> iterator()
```

Returns an `https://developer.android.com/reference/java/util/Iterator.html` that iterators over the selection, \*excluding\* any provisional selection.

### size

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

```
public int size()
```

| Returns |
|---|---|
| `int` | size of the selection including both final and provisional selected items. |

### toString

```
public String toString()
```