Class ImmutableSortedMultiset<E>
- All Implemented Interfaces:
Multiset<E>,SortedIterable<E>,SortedMultiset<E>,SortedMultisetBridge<E>,Serializable,Iterable<E>,Collection<E>
- Direct Known Subclasses:
DescendingImmutableSortedMultiset,RegularImmutableSortedMultiset
SortedMultiset whose contents will never change, with many other important properties
detailed at ImmutableCollection.
Warning: as with any sorted collection, you are strongly advised not to use a Comparator or Comparable type whose comparison behavior is inconsistent with
equals. That is, a.compareTo(b) or comparator.compare(a, b) should equal zero
if and only if a.equals(b). If this advice is not followed, the resulting
collection will not correctly obey its specification.
See the Guava User Guide article on immutable collections.
- Since:
- 12.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA builder for creating immutable multiset instances, especiallypublic static finalmultisets ("constant multisets").private static final classNested classes/interfaces inherited from class com.google.common.collect.ImmutableMultiset
ImmutableMultiset.ElementSet<E>, ImmutableMultiset.EntrySetSerializedForm<E>Nested classes/interfaces inherited from interface com.google.common.collect.Multiset
Multiset.Entry<E> -
Field Summary
FieldsFields inherited from class com.google.common.collect.ImmutableCollection
SPLITERATOR_CHARACTERISTICS -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal Comparator<? super E>Returns the comparator that orders this multiset, orOrdering.natural()if the natural ordering of the elements is used.static <E extends Comparable<? super E>>
ImmutableSortedMultiset<E>copyOf(E[] elements) Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.static <E> ImmutableSortedMultiset<E>Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.static <E> ImmutableSortedMultiset<E>copyOf(Comparator<? super E> comparator, Iterable<? extends E> elements) Returns an immutable sorted multiset containing the given elements sorted by the givenComparator.static <E> ImmutableSortedMultiset<E>copyOf(Comparator<? super E> comparator, Iterator<? extends E> elements) Returns an immutable sorted multiset containing the given elements sorted by the givenComparator.static <E> ImmutableSortedMultiset<E>Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.static <E> ImmutableSortedMultiset<E>copyOfSorted(SortedMultiset<E> sortedMultiset) Returns an immutable sorted multiset containing the elements of a sorted multiset, sorted by the sameComparator.private static <E> ImmutableSortedMultiset<E>copyOfSortedEntries(Comparator<? super E> comparator, Collection<Multiset.Entry<E>> entries) Returns a descending view of this multiset.abstract ImmutableSortedSet<E>Returns the set of distinct elements contained in this multiset.(package private) static <E> ImmutableSortedMultiset<E>emptyMultiset(Comparator<? super E> comparator) abstract ImmutableSortedMultiset<E>headMultiset(E upperBound, BoundType boundType) Returns a view of this multiset restricted to the elements less thanupperBound, optionally includingupperBounditself.static <E extends Comparable<?>>
ImmutableSortedMultiset.Builder<E>Returns a builder that creates immutable sorted multisets whose elements are ordered by their natural ordering.static <E> ImmutableSortedMultiset<E>of()Returns the empty immutable sorted multiset.static <E extends Comparable<? super E>>
ImmutableSortedMultiset<E>of(E element) Returns an immutable sorted multiset containing a single element.static <E extends Comparable<? super E>>
ImmutableSortedMultiset<E>of(E e1, E e2) Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.static <E extends Comparable<? super E>>
ImmutableSortedMultiset<E>of(E e1, E e2, E e3) Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.static <E extends Comparable<? super E>>
ImmutableSortedMultiset<E>of(E e1, E e2, E e3, E e4) Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.static <E extends Comparable<? super E>>
ImmutableSortedMultiset<E>of(E e1, E e2, E e3, E e4, E e5) Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.static <E extends Comparable<? super E>>
ImmutableSortedMultiset<E>of(E e1, E e2, E e3, E e4, E e5, E e6, E... remaining) Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.static <E> ImmutableSortedMultiset.Builder<E>orderedBy(Comparator<E> comparator) Returns a builder that creates immutable sorted multisets with an explicit comparator.final Multiset.Entry<E>Deprecated.Unsupported operation.final Multiset.Entry<E>Deprecated.Unsupported operation.private voidreadObject(ObjectInputStream stream) static <E extends Comparable<?>>
ImmutableSortedMultiset.Builder<E>Returns a builder that creates immutable sorted multisets whose elements are ordered by the reverse of their natural ordering.subMultiset(E lowerBound, BoundType lowerBoundType, E upperBound, BoundType upperBoundType) Returns a view of this multiset restricted to the range betweenlowerBoundandupperBound.abstract ImmutableSortedMultiset<E>tailMultiset(E lowerBound, BoundType boundType) Returns a view of this multiset restricted to the elements greater thanlowerBound, optionally includinglowerBounditself.static <E> Collector<E,?, ImmutableSortedMultiset<E>> toImmutableSortedMultiset(Comparator<? super E> comparator) Returns aCollectorthat accumulates the input elements into a newImmutableMultiset.static <T,E> Collector<T, ?, ImmutableSortedMultiset<E>> toImmutableSortedMultiset(Comparator<? super E> comparator, Function<? super T, ? extends E> elementFunction, ToIntFunction<? super T> countFunction) Returns aCollectorthat accumulates elements into anImmutableSortedMultisetwhose elements are the result of applyingelementFunctionto the inputs, with counts equal to the result of applyingcountFunctionto the inputs.(package private) ObjectMethods inherited from class com.google.common.collect.ImmutableSortedMultisetFauxverideShim
builder, copyOf, of, of, of, of, of, of, toImmutableMultiset, toImmutableMultisetMethods inherited from class com.google.common.collect.ImmutableMultiset
add, asList, contains, copyFromEntries, copyIntoArray, entrySet, equals, getEntry, hashCode, iterator, remove, setCount, setCount, toStringMethods inherited from class com.google.common.collect.ImmutableCollection
add, addAll, clear, internalArray, internalArrayEnd, internalArrayStart, isPartialView, remove, removeAll, removeIf, retainAll, spliterator, toArray, toArrayMethods inherited from class java.util.AbstractCollection
containsAll, isEmpty, sizeMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
addAll, clear, isEmpty, parallelStream, removeIf, stream, toArray, toArray, toArrayMethods inherited from interface com.google.common.collect.Multiset
add, add, contains, containsAll, count, equals, forEach, forEachEntry, hashCode, remove, remove, removeAll, retainAll, setCount, setCount, size, spliterator, toStringMethods inherited from interface com.google.common.collect.SortedMultiset
entrySet, firstEntry, iterator, lastEntry
-
Field Details
-
descendingMultiset
-
-
Constructor Details
-
ImmutableSortedMultiset
ImmutableSortedMultiset()
-
-
Method Details
-
toImmutableSortedMultiset
public static <E> Collector<E,?, toImmutableSortedMultisetImmutableSortedMultiset<E>> (Comparator<? super E> comparator) Returns aCollectorthat accumulates the input elements into a newImmutableMultiset. Elements are sorted by the specified comparator.Warning:
comparatorshould be consistent withequalsas explained in theComparatordocumentation.- Since:
- 21.0
-
toImmutableSortedMultiset
public static <T,E> Collector<T,?, toImmutableSortedMultisetImmutableSortedMultiset<E>> (Comparator<? super E> comparator, Function<? super T, ? extends E> elementFunction, ToIntFunction<? super T> countFunction) Returns aCollectorthat accumulates elements into anImmutableSortedMultisetwhose elements are the result of applyingelementFunctionto the inputs, with counts equal to the result of applyingcountFunctionto the inputs.If the mapped elements contain duplicates (according to
comparator), the first occurrence in encounter order appears in the resulting multiset, with count equal to the sum of the outputs ofcountFunction.applyAsInt(t)for eachtmapped to that element.- Since:
- 22.0
-
of
Returns the empty immutable sorted multiset.Performance note: the instance returned is a singleton.
-
of
Returns an immutable sorted multiset containing a single element. -
of
Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.- Throws:
NullPointerException- if any element is null
-
of
Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.- Throws:
NullPointerException- if any element is null
-
of
public static <E extends Comparable<? super E>> ImmutableSortedMultiset<E> of(E e1, E e2, E e3, E e4) Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.- Throws:
NullPointerException- if any element is null
-
of
public static <E extends Comparable<? super E>> ImmutableSortedMultiset<E> of(E e1, E e2, E e3, E e4, E e5) Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.- Throws:
NullPointerException- if any element is null
-
of
public static <E extends Comparable<? super E>> ImmutableSortedMultiset<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E... remaining) Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.- Throws:
NullPointerException- if any element is null
-
copyOf
Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.- Throws:
NullPointerException- if any ofelementsis null
-
copyOf
Returns an immutable sorted multiset containing the given elements sorted by their natural ordering. To create a copy of aSortedMultisetthat preserves the comparator, callcopyOfSorted(com.google.common.collect.SortedMultiset<E>)instead. This method iterates overelementsat most once.Note that if
sis aMultiset<String>, thenImmutableSortedMultiset.copyOf(s)returns anImmutableSortedMultiset<String>containing each of the strings ins, whileImmutableSortedMultiset.of(s)returns anImmutableSortedMultiset<Multiset<String>>containing one element (the given multiset itself).Despite the method name, this method attempts to avoid actually copying the data when it is safe to do so. The exact circumstances under which a copy will or will not be performed are undocumented and subject to change.
This method is not type-safe, as it may be called on elements that are not mutually comparable.
- Throws:
ClassCastException- if the elements are not mutually comparableNullPointerException- if any ofelementsis null
-
copyOf
Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.This method is not type-safe, as it may be called on elements that are not mutually comparable.
- Throws:
ClassCastException- if the elements are not mutually comparableNullPointerException- if any ofelementsis null
-
copyOf
public static <E> ImmutableSortedMultiset<E> copyOf(Comparator<? super E> comparator, Iterator<? extends E> elements) Returns an immutable sorted multiset containing the given elements sorted by the givenComparator.- Throws:
NullPointerException- ifcomparatoror any ofelementsis null
-
copyOf
public static <E> ImmutableSortedMultiset<E> copyOf(Comparator<? super E> comparator, Iterable<? extends E> elements) Returns an immutable sorted multiset containing the given elements sorted by the givenComparator. This method iterates overelementsat most once.Despite the method name, this method attempts to avoid actually copying the data when it is safe to do so. The exact circumstances under which a copy will or will not be performed are undocumented and subject to change.
- Throws:
NullPointerException- ifcomparatoror any ofelementsis null
-
copyOfSorted
Returns an immutable sorted multiset containing the elements of a sorted multiset, sorted by the sameComparator. That behavior differs fromcopyOf(Iterable), which always uses the natural ordering of the elements.Despite the method name, this method attempts to avoid actually copying the data when it is safe to do so. The exact circumstances under which a copy will or will not be performed are undocumented and subject to change.
This method is safe to use even when
sortedMultisetis a synchronized or concurrent collection that is currently being modified by another thread.- Throws:
NullPointerException- ifsortedMultisetor any of its elements is null
-
copyOfSortedEntries
private static <E> ImmutableSortedMultiset<E> copyOfSortedEntries(Comparator<? super E> comparator, Collection<Multiset.Entry<E>> entries) -
emptyMultiset
-
comparator
Description copied from interface:SortedMultisetReturns the comparator that orders this multiset, orOrdering.natural()if the natural ordering of the elements is used.- Specified by:
comparatorin interfaceSortedIterable<E>- Specified by:
comparatorin interfaceSortedMultiset<E>
-
elementSet
Description copied from interface:MultisetReturns the set of distinct elements contained in this multiset. The element set is backed by the same data as the multiset, so any change to either is immediately reflected in the other. The order of the elements in the element set is unspecified.If the element set supports any removal operations, these necessarily cause all occurrences of the removed element(s) to be removed from the multiset. Implementations are not expected to support the add operations, although this is possible.
A common use for the element set is to find the number of distinct elements in the multiset:
elementSet().size().- Specified by:
elementSetin interfaceMultiset<E>- Specified by:
elementSetin interfaceSortedMultiset<E>- Specified by:
elementSetin interfaceSortedMultisetBridge<E>- Specified by:
elementSetin classImmutableMultiset<E>- Returns:
- a view of the set of distinct elements in this multiset
-
descendingMultiset
Description copied from interface:SortedMultisetReturns a descending view of this multiset. Modifications made to either map will be reflected in the other.- Specified by:
descendingMultisetin interfaceSortedMultiset<E>
-
pollFirstEntry
Deprecated.Unsupported operation.Returns and removes the entry associated with the lowest element in this multiset, or returnsnullif this multiset is empty.This implementation is guaranteed to throw an
UnsupportedOperationException.- Specified by:
pollFirstEntryin interfaceSortedMultiset<E>- Throws:
UnsupportedOperationException- always
-
pollLastEntry
Deprecated.Unsupported operation.Returns and removes the entry associated with the greatest element in this multiset, or returnsnullif this multiset is empty.This implementation is guaranteed to throw an
UnsupportedOperationException.- Specified by:
pollLastEntryin interfaceSortedMultiset<E>- Throws:
UnsupportedOperationException- always
-
headMultiset
Description copied from interface:SortedMultisetReturns a view of this multiset restricted to the elements less thanupperBound, optionally includingupperBounditself. The returned multiset is a view of this multiset, so changes to one will be reflected in the other. The returned multiset supports all operations that this multiset supports.The returned multiset will throw an
IllegalArgumentExceptionon attempts to add elements outside its range.- Specified by:
headMultisetin interfaceSortedMultiset<E>
-
subMultiset
public ImmutableSortedMultiset<E> subMultiset(E lowerBound, BoundType lowerBoundType, E upperBound, BoundType upperBoundType) Description copied from interface:SortedMultisetReturns a view of this multiset restricted to the range betweenlowerBoundandupperBound. The returned multiset is a view of this multiset, so changes to one will be reflected in the other. The returned multiset supports all operations that this multiset supports.The returned multiset will throw an
IllegalArgumentExceptionon attempts to add elements outside its range.This method is equivalent to
tailMultiset(lowerBound, lowerBoundType).headMultiset(upperBound, upperBoundType).- Specified by:
subMultisetin interfaceSortedMultiset<E>
-
tailMultiset
Description copied from interface:SortedMultisetReturns a view of this multiset restricted to the elements greater thanlowerBound, optionally includinglowerBounditself. The returned multiset is a view of this multiset, so changes to one will be reflected in the other. The returned multiset supports all operations that this multiset supports.The returned multiset will throw an
IllegalArgumentExceptionon attempts to add elements outside its range.- Specified by:
tailMultisetin interfaceSortedMultiset<E>
-
orderedBy
Returns a builder that creates immutable sorted multisets with an explicit comparator. If the comparator has a more general type than the set being generated, such as creating aSortedMultiset<Integer>with aComparator<Number>, use theImmutableSortedMultiset.Builderconstructor instead.- Throws:
NullPointerException- ifcomparatoris null
-
reverseOrder
Returns a builder that creates immutable sorted multisets whose elements are ordered by the reverse of their natural ordering.Note: the type parameter
EextendsComparable<?>rather thanComparable<? super E>as a workaround for javac bug 6468354. -
naturalOrder
Returns a builder that creates immutable sorted multisets whose elements are ordered by their natural ordering. The sorted multisets useOrdering.natural()as the comparator. This method provides more type-safety thanImmutableSortedMultisetFauxverideShim.builder(), as it can be called only for classes that implementComparable.Note: the type parameter
EextendsComparable<?>rather thanComparable<? super E>as a workaround for javac bug 6468354. -
writeReplace
Object writeReplace()- Overrides:
writeReplacein classImmutableMultiset<E>
-
readObject
- Throws:
InvalidObjectException
-