# CopyOnWriteMultiset

Artifact: [androidx.media3:media3-common](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/common/util/CopyOnWriteMultiset.java+class:androidx.media3.common.util.CopyOnWriteMultiset)

*** ** * ** ***


```
@UnstableApi
class CopyOnWriteMultiset<E : Any?> : (Mutable)Iterable<E!>
```

<br />

*** ** * ** ***

An unordered collection of elements that allows duplicates, but also allows access to a set of unique elements.

This class is thread-safe using the same method as `https://developer.android.com/reference/java/util/concurrent/CopyOnWriteArrayList.html`. Mutation methods cause the underlying data to be copied. `https://developer.android.com/reference/kotlin/androidx/media3/common/util/CopyOnWriteMultiset#elementSet()` and `https://developer.android.com/reference/kotlin/androidx/media3/common/util/CopyOnWriteMultiset#iterator()` return snapshots that are unaffected by subsequent mutations.

Iterating directly on this class reveals duplicate elements. Unique elements can be accessed via `https://developer.android.com/reference/kotlin/androidx/media3/common/util/CopyOnWriteMultiset#elementSet()`. Iteration order for both of these is not defined.

| Parameters |
|---|---|
| `<E : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?>` | The type of element being stored. |

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/common/util/CopyOnWriteMultiset#CopyOnWriteMultiset()()` |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/CopyOnWriteMultiset#add(E)(element: E!)` Adds `element` to the multiset. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/CopyOnWriteMultiset#count(E)(element: E!)` Returns the number of occurrences of an element in this multiset. |
| `(https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-set/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-set/index.html<E!>!` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/CopyOnWriteMultiset#elementSet()()` Returns a snapshot of the unique elements currently in this multiset. |
| `(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<E!>!` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/CopyOnWriteMultiset#iterator()()` Returns an iterator over a snapshot of all the elements currently in this multiset (including duplicates). |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/CopyOnWriteMultiset#remove(E)(element: E!)` Removes `element` from the multiset. |

### 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!>!)` `https://developer.android.com/reference/java/util/Spliterator.html<T!>!` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-iterable/spliterator.html()`

## Public constructors

### CopyOnWriteMultiset

```
CopyOnWriteMultiset()
```

## Public functions

### add

```
fun add(element: E!): Unit
```

Adds `element` to the multiset.

| Parameters |
|---|---|
| `element: E!` | The element to be added. |

### count

```
fun count(element: E!): Int
```

Returns the number of occurrences of an element in this multiset.

### elementSet

```
fun elementSet(): (Mutable)Set<E!>!
```

Returns a snapshot of the unique elements currently in this multiset.

Changes to the underlying multiset are not reflected in the returned value.

| Returns |
|---|---|
| `(https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-set/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-set/index.html<E!>!` | An unmodifiable set containing the unique elements in this multiset. |

### iterator

```
fun iterator(): (Mutable)Iterator<E!>!
```

Returns an iterator over a snapshot of all the elements currently in this multiset (including duplicates).

Changes to the underlying multiset are not reflected in the returned value.

| Returns |
|---|---|
| `(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<E!>!` | An unmodifiable iterator over all the elements in this multiset (including duplicates). |

### remove

```
fun remove(element: E!): Unit
```

Removes `element` from the multiset.

| Parameters |
|---|---|
| `element: E!` | The element to be removed. |