CopyOnWriteMultiset


@UnstableApi
public final class CopyOnWriteMultiset<E extends Object> implements Iterable


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 . Mutation methods cause the underlying data to be copied. elementSet and iterator return snapshots that are unaffected by subsequent mutations.

Iterating directly on this class reveals duplicate elements. Unique elements can be accessed via elementSet. Iteration order for both of these is not defined.

Parameters
<E extends Object>

The type of element being stored.

Summary

Public constructors

Public methods

void
add(E element)

Adds element to the multiset.

int
count(E element)

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

Set<E>

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

Iterator<E>

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

void
remove(E element)

Removes element from the multiset.

Inherited methods

From java.lang.Iterable
void
forEach(Consumer<T> action)
Spliterator<T>

Public constructors

CopyOnWriteMultiset

public CopyOnWriteMultiset()

Public methods

add

public void add(E element)

Adds element to the multiset.

Parameters
E element

The element to be added.

count

public int count(E element)

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

elementSet

public Set<E> elementSet()

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

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

Returns
Set<E>

An unmodifiable set containing the unique elements in this multiset.

iterator

public Iterator<E> iterator()

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
Iterator<E>

An unmodifiable iterator over all the elements in this multiset (including duplicates).

remove

public void remove(E element)

Removes element from the multiset.

Parameters
E element

The element to be removed.