Class ImmutableSortedSet<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- com.google.common.collect.ImmutableCollection<E>
-
- com.google.common.collect.ImmutableSet<E>
-
- com.google.common.collect.ImmutableSortedSet<E>
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Iterable<E>,java.util.Collection<E>,java.util.NavigableSet<E>,java.util.Set<E>,java.util.SortedSet<E>
- Direct Known Subclasses:
ContiguousSet
@GwtCompatible(serializable=true, emulated=true) public abstract class ImmutableSortedSet<E> extends ImmutableSet<E> implements java.util.NavigableSet<E>
An immutableSortedSetthat stores its elements in a sorted array. Some instances are ordered by an explicit comparator, while others follow the natural sort ordering of their elements. Either way, null elements are not supported.Unlike
Collections.unmodifiableSortedSet(java.util.SortedSet<T>), which is a view of a separate collection that can still change, an instance ofImmutableSortedSetcontains its own private data and will never change. This class is convenient forpublic static finalsets ("constant sets") and also lets you easily make a "defensive copy" of a set provided to your class by a caller.The sets returned by the
headSet(E),tailSet(E), andsubSet(E, E)methods share the same array as the original set, preventing that array from being garbage collected. If this is a concern, the data may be copied into a correctly-sized array by callingcopyOfSorted(java.util.SortedSet<E>).Note on element equivalence: The
ImmutableCollection.contains(Object),AbstractCollection.containsAll(Collection), andImmutableSet.equals(Object)implementations must check whether a provided object is equivalent to an element in the collection. Unlike most collections, anImmutableSortedSetdoesn't useObject.equals(java.lang.Object)to determine if two elements are equivalent. Instead, with an explicit comparator, the following relation determines whether elementsxandyare equivalent:{(x, y) | comparator.compare(x, y) == 0}With natural ordering of elements, the following relation determines whether two elements are equivalent:
Warning: Like most sets, an{(x, y) | x.compareTo(y) == 0}ImmutableSortedSetwill not function correctly if an element is modified after being placed in the set. For this reason, and to avoid general confusion, it is strongly recommended to place only immutable objects into this collection.Note: Although this class is not final, it cannot be subclassed as it has no public or protected constructors. Thus, instances of this type are guaranteed to be immutable.
See the Guava User Guide article on immutable collections.
- Since:
- 2.0 (imported from Google Collections Library; implements
NavigableSetsince 12.0) - See Also:
ImmutableSet, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classImmutableSortedSet.Builder<E>A builder for creating immutable sorted set instances, especiallypublic static finalsets ("constant sets"), with a given comparator.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <E> ImmutableSortedSet.Builder<E>builder()Deprecated.UsenaturalOrder(), which offers better type-safety.Eceiling(E e)java.util.Comparator<? super E>comparator()Returns the comparator that orders the elements, which isOrdering.natural()when the natural ordering of the elements is used.static <E extends java.lang.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>copyOf(E[] elements)Deprecated.Pass parameters of typeComparableto usecopyOf(Comparable[]).static <E> ImmutableSortedSet<E>copyOf(java.lang.Iterable<? extends E> elements)Returns an immutable sorted set containing the given elements sorted by their natural ordering.static <E> ImmutableSortedSet<E>copyOf(java.util.Collection<? extends E> elements)Returns an immutable sorted set containing the given elements sorted by their natural ordering.static <E> ImmutableSortedSet<E>copyOf(java.util.Comparator<? super E> comparator, java.lang.Iterable<? extends E> elements)Returns an immutable sorted set containing the given elements sorted by the givenComparator.static <E> ImmutableSortedSet<E>copyOf(java.util.Comparator<? super E> comparator, java.util.Collection<? extends E> elements)Returns an immutable sorted set containing the given elements sorted by the givenComparator.static <E> ImmutableSortedSet<E>copyOf(java.util.Comparator<? super E> comparator, java.util.Iterator<? extends E> elements)Returns an immutable sorted set containing the given elements sorted by the givenComparator.static <E> ImmutableSortedSet<E>copyOf(java.util.Iterator<? extends E> elements)Returns an immutable sorted set containing the given elements sorted by their natural ordering.static <E> ImmutableSortedSet<E>copyOfSorted(java.util.SortedSet<E> sortedSet)Returns an immutable sorted set containing the elements of a sorted set, sorted by the sameComparator.abstract UnmodifiableIterator<E>descendingIterator()ImmutableSortedSet<E>descendingSet()Efirst()Efloor(E e)ImmutableSortedSet<E>headSet(E toElement)ImmutableSortedSet<E>headSet(E toElement, boolean inclusive)Ehigher(E e)abstract UnmodifiableIterator<E>iterator()Returns an unmodifiable iterator across the elements in this collection.Elast()Elower(E e)static <E extends java.lang.Comparable<?>>
ImmutableSortedSet.Builder<E>naturalOrder()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 java.lang.Comparable<? super E>>
ImmutableSortedSet<E>of(E element)Returns an immutable sorted set containing a single element.static <E> ImmutableSortedSet<E>of(E element)Deprecated.Pass a parameter of typeComparableto useof(Comparable).static <E extends java.lang.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 java.lang.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 java.lang.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 java.lang.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 java.lang.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(java.util.Comparator<E> comparator)Returns a builder that creates immutable sorted sets with an explicit comparator.EpollFirst()Deprecated.Unsupported operation.EpollLast()Deprecated.Unsupported operation.static <E extends java.lang.Comparable<?>>
ImmutableSortedSet.Builder<E>reverseOrder()Returns a builder that creates immutable sorted sets whose elements are ordered by the reverse of their natural ordering.ImmutableSortedSet<E>subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)ImmutableSortedSet<E>subSet(E fromElement, E toElement)ImmutableSortedSet<E>tailSet(E fromElement)ImmutableSortedSet<E>tailSet(E fromElement, boolean inclusive)-
Methods inherited from class com.google.common.collect.ImmutableSet
equals, hashCode
-
Methods inherited from class com.google.common.collect.ImmutableCollection
add, addAll, asList, clear, contains, remove, removeAll, retainAll, toArray, toArray
-
-
-
-
Method Detail
-
of
public static <E> ImmutableSortedSet<E> of()
Returns the empty immutable sorted set.
-
of
public static <E extends java.lang.Comparable<? super E>> ImmutableSortedSet<E> of(E element)
Returns an immutable sorted set containing a single element.
-
of
public static <E extends java.lang.Comparable<? super E>> ImmutableSortedSet<E> of(E e1, E e2)
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:
java.lang.NullPointerException- if any element is null
-
of
public static <E extends java.lang.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. When multiple elements are equivalent according toComparable.compareTo(T), only the first one specified is included.- Throws:
java.lang.NullPointerException- if any element is null
-
of
public static <E extends java.lang.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. When multiple elements are equivalent according toComparable.compareTo(T), only the first one specified is included.- Throws:
java.lang.NullPointerException- if any element is null
-
of
public static <E extends java.lang.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:
java.lang.NullPointerException- if any element is null
-
of
public static <E extends java.lang.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:
java.lang.NullPointerException- if any element is null- Since:
- 3.0 (source-compatible since 2.0)
-
copyOf
public static <E extends java.lang.Comparable<? super E>> ImmutableSortedSet<E> copyOf(E[] elements)
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:
java.lang.NullPointerException- if any ofelementsis null- Since:
- 3.0
-
copyOf
public static <E> ImmutableSortedSet<E> copyOf(java.lang.Iterable<? extends E> elements)
Returns 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:
java.lang.ClassCastException- if the elements are not mutually comparablejava.lang.NullPointerException- if any ofelementsis null
-
copyOf
public static <E> ImmutableSortedSet<E> copyOf(java.util.Collection<? extends E> elements)
Returns 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:
java.lang.ClassCastException- if the elements are not mutually comparablejava.lang.NullPointerException- if any ofelementsis null- Since:
- 7.0 (source-compatible since 2.0)
-
copyOf
public static <E> ImmutableSortedSet<E> copyOf(java.util.Iterator<? extends E> elements)
Returns 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:
java.lang.ClassCastException- if the elements are not mutually comparablejava.lang.NullPointerException- if any ofelementsis null
-
copyOf
public static <E> ImmutableSortedSet<E> copyOf(java.util.Comparator<? super E> comparator, java.util.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:
java.lang.NullPointerException- ifcomparatoror any ofelementsis null
-
copyOf
public static <E> ImmutableSortedSet<E> copyOf(java.util.Comparator<? super E> comparator, java.lang.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:
java.lang.NullPointerException- ifcomparatoror any ofelementsis null
-
copyOf
public static <E> ImmutableSortedSet<E> copyOf(java.util.Comparator<? super E> comparator, java.util.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:
java.lang.NullPointerException- ifcomparatoror any ofelementsis null- Since:
- 7.0 (source-compatible since 2.0)
-
copyOfSorted
public static <E> ImmutableSortedSet<E> copyOfSorted(java.util.SortedSet<E> sortedSet)
Returns 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:
java.lang.NullPointerException- ifsortedSetor any of its elements is null
-
orderedBy
public static <E> ImmutableSortedSet.Builder<E> orderedBy(java.util.Comparator<E> comparator)
Returns 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:
java.lang.NullPointerException- ifcomparatoris null
-
reverseOrder
public static <E extends java.lang.Comparable<?>> ImmutableSortedSet.Builder<E> reverseOrder()
Returns a builder that creates immutable sorted sets whose elements are ordered by the reverse of their natural ordering.
-
naturalOrder
public static <E extends java.lang.Comparable<?>> ImmutableSortedSet.Builder<E> naturalOrder()
Returns 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.
-
comparator
public java.util.Comparator<? super E> comparator()
Returns 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 interfacejava.util.SortedSet<E>
-
iterator
public abstract UnmodifiableIterator<E> iterator()
Description copied from class:ImmutableCollectionReturns an unmodifiable iterator across the elements in this collection.
-
headSet
public ImmutableSortedSet<E> headSet(E toElement)
This 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
@GwtIncompatible("NavigableSet") public ImmutableSortedSet<E> headSet(E toElement, boolean inclusive)
- Specified by:
headSetin interfacejava.util.NavigableSet<E>- Since:
- 12.0
-
subSet
public ImmutableSortedSet<E> subSet(E fromElement, E toElement)
This 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("NavigableSet") public ImmutableSortedSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
- Specified by:
subSetin interfacejava.util.NavigableSet<E>- Since:
- 12.0
-
tailSet
public ImmutableSortedSet<E> tailSet(E fromElement)
This 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
@GwtIncompatible("NavigableSet") public ImmutableSortedSet<E> tailSet(E fromElement, boolean inclusive)
- Specified by:
tailSetin interfacejava.util.NavigableSet<E>- Since:
- 12.0
-
lower
@GwtIncompatible("NavigableSet") public E lower(E e)
- Specified by:
lowerin interfacejava.util.NavigableSet<E>- Since:
- 12.0
-
floor
@GwtIncompatible("NavigableSet") public E floor(E e)
- Specified by:
floorin interfacejava.util.NavigableSet<E>- Since:
- 12.0
-
ceiling
@GwtIncompatible("NavigableSet") public E ceiling(E e)
- Specified by:
ceilingin interfacejava.util.NavigableSet<E>- Since:
- 12.0
-
higher
@GwtIncompatible("NavigableSet") public E higher(E e)
- Specified by:
higherin interfacejava.util.NavigableSet<E>- Since:
- 12.0
-
pollFirst
@Deprecated @GwtIncompatible("NavigableSet") public final E pollFirst()
Deprecated.Unsupported operation.Guaranteed to throw an exception and leave the set unmodified.- Specified by:
pollFirstin interfacejava.util.NavigableSet<E>- Throws:
java.lang.UnsupportedOperationException- always- Since:
- 12.0
-
pollLast
@Deprecated @GwtIncompatible("NavigableSet") public final E pollLast()
Deprecated.Unsupported operation.Guaranteed to throw an exception and leave the set unmodified.- Specified by:
pollLastin interfacejava.util.NavigableSet<E>- Throws:
java.lang.UnsupportedOperationException- always- Since:
- 12.0
-
descendingSet
@GwtIncompatible("NavigableSet") public ImmutableSortedSet<E> descendingSet()
- Specified by:
descendingSetin interfacejava.util.NavigableSet<E>- Since:
- 12.0
-
descendingIterator
@GwtIncompatible("NavigableSet") public abstract UnmodifiableIterator<E> descendingIterator()
- Specified by:
descendingIteratorin interfacejava.util.NavigableSet<E>- Since:
- 12.0
-
builder
@Deprecated public static <E> ImmutableSortedSet.Builder<E> builder()
Deprecated.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:
java.lang.UnsupportedOperationException- always
-
of
@Deprecated public static <E> ImmutableSortedSet<E> of(E element)
Deprecated.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:
java.lang.UnsupportedOperationException- always
-
of
@Deprecated public static <E> ImmutableSortedSet<E> of(E e1, E e2)
Deprecated.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:
java.lang.UnsupportedOperationException- always
-
of
@Deprecated public static <E> ImmutableSortedSet<E> of(E e1, E e2, E e3)
Deprecated.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:
java.lang.UnsupportedOperationException- always
-
of
@Deprecated public static <E> ImmutableSortedSet<E> of(E e1, E e2, E e3, E e4)
Deprecated.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:
java.lang.UnsupportedOperationException- always
-
of
@Deprecated public 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).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:
java.lang.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:
java.lang.UnsupportedOperationException- always
-
copyOf
@Deprecated public static <E> ImmutableSortedSet<E> copyOf(E[] elements)
Deprecated.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:
java.lang.UnsupportedOperationException- always
-
-