Class ImmutableSortedSet<E>
- All Implemented Interfaces:
- Serializable,- Iterable<E>,- Collection<E>,- NavigableSet<E>,- SequencedCollection<E>,- SequencedSet<E>,- Set<E>,- SortedSet<E>
- Direct Known Subclasses:
- ContiguousSet
NavigableSet 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:
- 2.0 (implements NavigableSetsince 12.0)
- Author:
- Jared Levy, Louis Wasserman
- See Also:
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic final classA builder for creating immutable sorted set instances, especiallypublic static finalsets ("constant sets"), with a given comparator.
- 
Method SummaryModifier and TypeMethodDescriptionstatic <E> ImmutableSortedSet.Builder<E> builder()Deprecated.static <E> ImmutableSortedSet.Builder<E> builderWithExpectedSize(int expectedSize) Deprecated.Not supported by ImmutableSortedSet.Comparator<? super E> Returns the comparator that orders the elements, which isOrdering.natural()when the natural ordering of the elements is used.static <E extends Comparable<? super E>>
 ImmutableSortedSet<E> copyOf(E[] elements) Returns an immutable sorted set containing the given elements sorted by their natural ordering.static <E> ImmutableSortedSet<E> Returns an immutable sorted set containing the given elements sorted by their natural ordering.static <E> ImmutableSortedSet<E> copyOf(Collection<? extends E> elements) Returns an immutable sorted set containing the given elements sorted by their natural ordering.static <E> ImmutableSortedSet<E> copyOf(Comparator<? super E> comparator, Iterable<? extends E> elements) Returns an immutable sorted set containing the given elements sorted by the givenComparator.static <E> ImmutableSortedSet<E> copyOf(Comparator<? super E> comparator, Collection<? extends E> elements) Returns an immutable sorted set containing the given elements sorted by the givenComparator.static <E> ImmutableSortedSet<E> copyOf(Comparator<? super E> comparator, Iterator<? extends E> elements) Returns an immutable sorted set containing the given elements sorted by the givenComparator.static <E> ImmutableSortedSet<E> Returns an immutable sorted set containing the given elements sorted by their natural ordering.static <Z> ImmutableSortedSet<Z> copyOf(Z[] elements) Deprecated.Pass parameters of typeComparableto usecopyOf(Comparable[]).static <E> ImmutableSortedSet<E> copyOfSorted(SortedSet<E> sortedSet) Returns an immutable sorted set containing the elements of a sorted set, sorted by the sameComparator.abstract UnmodifiableIterator<E> first()abstract UnmodifiableIterator<E> iterator()Returns an unmodifiable iterator across the elements in this collection.last()static <E extends Comparable<?>>
 ImmutableSortedSet.Builder<E> Returns a builder that creates immutable sorted sets whose elements are ordered by their natural ordering.static <E> ImmutableSortedSet<E> of()Returns the empty immutable sorted set.static <E extends Comparable<? super E>>
 ImmutableSortedSet<E> of(E e1) Returns an immutable sorted set containing a single element.static <E> ImmutableSortedSet<E> of(E e1) Deprecated.Pass a parameter of typeComparableto useof(Comparable).static <E extends Comparable<? super E>>
 ImmutableSortedSet<E> of(E e1, E e2) Returns an immutable sorted set containing the given elements sorted by their natural ordering.static <E> ImmutableSortedSet<E> of(E e1, E e2) Deprecated.Pass the parameters of typeComparableto useof(Comparable, Comparable).static <E extends Comparable<? super E>>
 ImmutableSortedSet<E> of(E e1, E e2, E e3) Returns an immutable sorted set containing the given elements sorted by their natural ordering.static <E> ImmutableSortedSet<E> of(E e1, E e2, E e3) Deprecated.Pass the parameters of typeComparableto useof(Comparable, Comparable, Comparable).static <E extends Comparable<? super E>>
 ImmutableSortedSet<E> of(E e1, E e2, E e3, E e4) Returns an immutable sorted set containing the given elements sorted by their natural ordering.static <E> ImmutableSortedSet<E> of(E e1, E e2, E e3, E e4) Deprecated.Pass the parameters of typeComparableto useof(Comparable, Comparable, Comparable, Comparable).static <E extends Comparable<? super E>>
 ImmutableSortedSet<E> of(E e1, E e2, E e3, E e4, E e5) Returns an immutable sorted set containing the given elements sorted by their natural ordering.static <E> ImmutableSortedSet<E> of(E e1, E e2, E e3, E e4, E e5) Deprecated.Pass the parameters of typeComparableto useof(Comparable, Comparable, Comparable, Comparable, Comparable).static <E extends Comparable<? super E>>
 ImmutableSortedSet<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E... remaining) Returns an immutable sorted set containing the given elements sorted by their natural ordering.static <E> ImmutableSortedSet<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E... remaining) Deprecated.Pass the parameters of typeComparableto useof(Comparable, Comparable, Comparable, Comparable, Comparable, Comparable, Comparable...).static <E> ImmutableSortedSet.Builder<E> orderedBy(Comparator<E> comparator) Returns a builder that creates immutable sorted sets with an explicit comparator.Deprecated.Unsupported operation.pollLast()Deprecated.Unsupported operation.static <E extends Comparable<?>>
 ImmutableSortedSet.Builder<E> Returns a builder that creates immutable sorted sets whose elements are ordered by the reverse of their natural ordering.static <E> Collector<E, ?, ImmutableSet<E>> Deprecated.static <E> Collector<E, ?, ImmutableSortedSet<E>> toImmutableSortedSet(Comparator<? super E> comparator) Returns aCollectorthat accumulates the input elements into a newImmutableSortedSet, ordered by the specified comparator.Methods inherited from class com.google.common.collect.ImmutableSetasList, equals, hashCodeMethods inherited from class com.google.common.collect.ImmutableCollectionadd, addAll, clear, contains, remove, removeAll, retainAll, spliterator, toArray, toArrayMethods inherited from class java.util.AbstractCollectioncontainsAll, isEmpty, size, toStringMethods inherited from class java.lang.Objectclone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.CollectionparallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.NavigableSetremoveFirst, removeLast, reversed
- 
Method Details- 
toImmutableSortedSetpublic static <E> Collector<E, ?, ImmutableSortedSet<E>> toImmutableSortedSet(Comparator<? super E> comparator) Returns aCollectorthat accumulates the input elements into a newImmutableSortedSet, ordered by the specified comparator.If the elements contain duplicates (according to the comparator), only the first duplicate in encounter order will appear in the result. - Since:
- 33.2.0 (available since 21.0 in guava-jre)
 
- 
ofReturns the empty immutable sorted set.Performance note: the instance returned is a singleton. 
- 
ofReturns an immutable sorted set containing a single element.
- 
ofReturns an immutable sorted set containing the given elements sorted by their natural ordering. When multiple elements are equivalent according toComparable.compareTo(T), only the first one specified is included.- Throws:
- NullPointerException- if any element is null
 
- 
ofReturns an immutable sorted set containing the given elements sorted by their natural ordering. When multiple elements are equivalent according toComparable.compareTo(T), only the first one specified is included.- Throws:
- NullPointerException- if any element is null
 
- 
ofReturns an immutable sorted set containing the given elements sorted by their natural ordering. When multiple elements are equivalent according toComparable.compareTo(T), only the first one specified is included.- Throws:
- NullPointerException- if any element is null
 
- 
ofpublic static <E extends Comparable<? super E>> ImmutableSortedSet<E> of(E e1, E e2, E e3, E e4, E e5) Returns an immutable sorted set containing the given elements sorted by their natural ordering. When multiple elements are equivalent according toComparable.compareTo(T), only the first one specified is included.- Throws:
- NullPointerException- if any element is null
 
- 
ofpublic static <E extends Comparable<? super E>> ImmutableSortedSet<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E... remaining) Returns an immutable sorted set containing the given elements sorted by their natural ordering. When multiple elements are equivalent according toComparable.compareTo(T), only the first one specified is included.- Throws:
- NullPointerException- if any element is null
- Since:
- 3.0 (source-compatible since 2.0)
 
- 
copyOfReturns an immutable sorted set containing the given elements sorted by their natural ordering. When multiple elements are equivalent according toComparable.compareTo(T), only the first one specified is included.- Throws:
- NullPointerException- if any of- elementsis null
- Since:
- 3.0
 
- 
copyOfReturns an immutable sorted set containing the given elements sorted by their natural ordering. When multiple elements are equivalent according tocompareTo(), only the first one specified is included. To create a copy of aSortedSetthat preserves the comparator, callcopyOfSorted(java.util.SortedSet<E>)instead. This method iterates overelementsat most once.Note that if sis aSet<String>, thenImmutableSortedSet.copyOf(s)returns anImmutableSortedSet<String>containing each of the strings ins, whileImmutableSortedSet.of(s)returns anImmutableSortedSet<Set<String>>containing one element (the given set 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 comparable
- NullPointerException- if any of- elementsis null
 
- 
copyOfReturns an immutable sorted set containing the given elements sorted by their natural ordering. When multiple elements are equivalent according tocompareTo(), only the first one specified is included. To create a copy of aSortedSetthat preserves the comparator, callcopyOfSorted(java.util.SortedSet<E>)instead. This method iterates overelementsat most once.Note that if sis aSet<String>, thenImmutableSortedSet.copyOf(s)returns anImmutableSortedSet<String>containing each of the strings ins, whileImmutableSortedSet.of(s)returns anImmutableSortedSet<Set<String>>containing one element (the given set itself).Note: Despite what the method name suggests, if elementsis anImmutableSortedSet, it may be returned instead of a copy.This method is not type-safe, as it may be called on elements that are not mutually comparable. This method is safe to use even when elementsis a synchronized or concurrent collection that is currently being modified by another thread.- Throws:
- ClassCastException- if the elements are not mutually comparable
- NullPointerException- if any of- elementsis null
- Since:
- 7.0 (source-compatible since 2.0)
 
- 
copyOfReturns an immutable sorted set containing the given elements sorted by their natural ordering. When multiple elements are equivalent according tocompareTo(), only the first one specified is included.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 comparable
- NullPointerException- if any of- elementsis null
 
- 
copyOfpublic static <E> ImmutableSortedSet<E> copyOf(Comparator<? super E> comparator, Iterator<? extends E> elements) Returns an immutable sorted set containing the given elements sorted by the givenComparator. When multiple elements are equivalent according tocompareTo(), only the first one specified is included.- Throws:
- NullPointerException- if- comparatoror any of- elementsis null
 
- 
copyOfpublic static <E> ImmutableSortedSet<E> copyOf(Comparator<? super E> comparator, Iterable<? extends E> elements) Returns an immutable sorted set containing the given elements sorted by the givenComparator. When multiple elements are equivalent according tocompare(), only the first one specified is included. 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- if- comparatoror any of- elementsis null
 
- 
copyOfpublic static <E> ImmutableSortedSet<E> copyOf(Comparator<? super E> comparator, Collection<? extends E> elements) Returns an immutable sorted set containing the given elements sorted by the givenComparator. When multiple elements are equivalent according tocompareTo(), only the first one specified is included.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 elementsis a synchronized or concurrent collection that is currently being modified by another thread.- Throws:
- NullPointerException- if- comparatoror any of- elementsis null
- Since:
- 7.0 (source-compatible since 2.0)
 
- 
copyOfSortedReturns an immutable sorted set containing the elements of a sorted set, 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 sortedSetis a synchronized or concurrent collection that is currently being modified by another thread.- Throws:
- NullPointerException- if- sortedSetor any of its elements is null
 
- 
orderedByReturns a builder that creates immutable sorted sets with an explicit comparator. If the comparator has a more general type than the set being generated, such as creating aSortedSet<Integer>with aComparator<Number>, use theImmutableSortedSet.Builderconstructor instead.- Throws:
- NullPointerException- if- comparatoris null
 
- 
reverseOrderReturns a builder that creates immutable sorted sets whose elements are ordered by the reverse of their natural ordering.
- 
naturalOrderReturns a builder that creates immutable sorted sets whose elements are ordered by their natural ordering. The sorted sets useOrdering.natural()as the comparator. This method provides more type-safety thanbuilder(), as it can be called only for classes that implementComparable.
- 
comparatorReturns the comparator that orders the elements, which isOrdering.natural()when the natural ordering of the elements is used. Note that its behavior is not consistent withSortedSet.comparator(), which returnsnullto indicate natural ordering.- Specified by:
- comparatorin interface- SortedSet<E>
 
- 
iteratorDescription copied from class:ImmutableCollectionReturns an unmodifiable iterator across the elements in this collection.- Specified by:
- iteratorin interface- Collection<E>
- Specified by:
- iteratorin interface- Iterable<E>
- Specified by:
- iteratorin interface- NavigableSet<E>
- Specified by:
- iteratorin interface- Set<E>
- Specified by:
- iteratorin class- ImmutableSet<E>
 
- 
headSetThis method returns a serializable ImmutableSortedSet.The SortedSet.headSet(E)documentation states that a subset of a subset throws anIllegalArgumentExceptionif passed atoElementgreater than an earliertoElement. However, this method doesn't throw an exception in that situation, but instead keeps the originaltoElement.
- 
headSet- Specified by:
- headSetin interface- NavigableSet<E>
- Since:
- 12.0
 
- 
subSetThis method returns a serializable ImmutableSortedSet.The SortedSet.subSet(E, E)documentation states that a subset of a subset throws anIllegalArgumentExceptionif passed afromElementsmaller than an earlierfromElement. However, this method doesn't throw an exception in that situation, but instead keeps the originalfromElement. Similarly, this method keeps the originaltoElement, instead of throwing an exception, if passed atoElementgreater than an earliertoElement.
- 
subSet@GwtIncompatible public ImmutableSortedSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) - Specified by:
- subSetin interface- NavigableSet<E>
- Since:
- 12.0
 
- 
tailSetThis method returns a serializable ImmutableSortedSet.The SortedSet.tailSet(E)documentation states that a subset of a subset throws anIllegalArgumentExceptionif passed afromElementsmaller than an earlierfromElement. However, this method doesn't throw an exception in that situation, but instead keeps the originalfromElement.
- 
tailSet- Specified by:
- tailSetin interface- NavigableSet<E>
- Since:
- 12.0
 
- 
lower- Specified by:
- lowerin interface- NavigableSet<E>
- Since:
- 12.0
 
- 
floor
- 
ceiling
- 
higher- Specified by:
- higherin interface- NavigableSet<E>
- Since:
- 12.0
 
- 
first
- 
last
- 
pollFirstDeprecated.Unsupported operation.Guaranteed to throw an exception and leave the set unmodified.- Specified by:
- pollFirstin interface- NavigableSet<E>
- Throws:
- UnsupportedOperationException- always
- Since:
- 12.0
 
- 
pollLastDeprecated.Unsupported operation.Guaranteed to throw an exception and leave the set unmodified.- Specified by:
- pollLastin interface- NavigableSet<E>
- Throws:
- UnsupportedOperationException- always
- Since:
- 12.0
 
- 
descendingSet- Specified by:
- descendingSetin interface- NavigableSet<E>
- Since:
- 12.0
 
- 
descendingIterator- Specified by:
- descendingIteratorin interface- NavigableSet<E>
- Since:
- 12.0
 
- 
toImmutableSetDeprecated.Not supported. UsetoImmutableSortedSet(java.util.Comparator<? super E>)instead. This method exists only to hideImmutableSet.toImmutableSet()from consumers ofImmutableSortedSet.- Throws:
- UnsupportedOperationException- always
- Since:
- 33.2.0 (available since 21.0 in guava-jre)
 
- 
builderDeprecated.UsenaturalOrder(), which offers better type-safety.Not supported. UsenaturalOrder(), which offers better type-safety, instead. This method exists only to hideImmutableSet.builder()from consumers ofImmutableSortedSet.- Throws:
- UnsupportedOperationException- always
 
- 
builderWithExpectedSize@Deprecated public static <E> ImmutableSortedSet.Builder<E> builderWithExpectedSize(int expectedSize) Deprecated.Not supported by ImmutableSortedSet.Not supported. This method exists only to hideImmutableSet.builderWithExpectedSize(int)from consumers ofImmutableSortedSet.- Throws:
- UnsupportedOperationException- always
 
- 
ofDeprecated.Pass a parameter of typeComparableto useof(Comparable).Not supported. You are attempting to create a set that may contain a non-Comparableelement. Proper calls will resolve to the version inImmutableSortedSet, not this dummy version.- Throws:
- UnsupportedOperationException- always
 
- 
ofDeprecated.Pass the parameters of typeComparableto useof(Comparable, Comparable).Not supported. You are attempting to create a set that may contain a non-Comparableelement. Proper calls will resolve to the version inImmutableSortedSet, not this dummy version.- Throws:
- UnsupportedOperationException- always
 
- 
ofDeprecated.Pass the parameters of typeComparableto useof(Comparable, Comparable, Comparable).Not supported. You are attempting to create a set that may contain a non-Comparableelement. Proper calls will resolve to the version inImmutableSortedSet, not this dummy version.- Throws:
- UnsupportedOperationException- always
 
- 
ofDeprecated.Pass the parameters of typeComparableto useof(Comparable, Comparable, Comparable, Comparable).Not supported. You are attempting to create a set that may contain a non-Comparableelement. Proper calls will resolve to the version inImmutableSortedSet, not this dummy version.- Throws:
- UnsupportedOperationException- always
 
- 
ofDeprecated.Pass the parameters of typeComparableto useof(Comparable, Comparable, Comparable, Comparable, Comparable).Not supported. You are attempting to create a set that may contain a non-Comparableelement. Proper calls will resolve to the version inImmutableSortedSet, not this dummy version.- Throws:
- UnsupportedOperationException- always
 
- 
of@Deprecated public static <E> ImmutableSortedSet<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E... remaining) Deprecated.Pass the parameters of typeComparableto useof(Comparable, Comparable, Comparable, Comparable, Comparable, Comparable, Comparable...).Not supported. You are attempting to create a set that may contain a non-Comparableelement. Proper calls will resolve to the version inImmutableSortedSet, not this dummy version.- Throws:
- UnsupportedOperationException- always
 
- 
copyOfDeprecated.Pass parameters of typeComparableto usecopyOf(Comparable[]).Not supported. You are attempting to create a set that may contain non-Comparableelements. Proper calls will resolve to the version inImmutableSortedSet, not this dummy version.- Throws:
- UnsupportedOperationException- always
 
 
- 
naturalOrder(), which offers better type-safety.