@GwtCompatible(serializable=true, emulated=true) public abstract class ImmutableSortedSet<E> extends ImmutableSet<E> implements NavigableSet <E>
SortedSet that 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, which is a view of a separate collection that can still change, an instance of ImmutableSortedSet contains its own private data and will never change. This class is convenient for public static final sets ("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), and subSet(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 calling copyOfSorted(java.util.SortedSet<E>).
Note on element equivalence: The ImmutableCollection, AbstractCollection, and ImmutableSet implementations must check whether a provided object is equivalent to an element in the collection. Unlike most collections, an ImmutableSortedSet doesn't use Object to determine if two elements are equivalent. Instead, with an explicit comparator, the following relation determines whether elements x and y are equivalent:
{(x, y) | comparator.compare(x, y) == 0}
With natural ordering of elements, the following relation determines whether two elements are equivalent:
{(x, y) | x.compareTo(y) == 0}
Warning: Like most sets, an
ImmutableSortedSet will 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.
NavigableSet since 12.0)
ImmutableSet,
Serialized Form
| Modifier and Type | Class and Description |
|---|---|
static class |
ImmutableSortedSet
A builder for creating immutable sorted set instances, especially
public static final sets ("constant sets"), with a given comparator.
|
| Modifier and Type | Method and Description |
|---|---|
static <E> ImmutableSortedSet |
builder()
Deprecated.
Use
naturalOrder(), which offers better type-safety.
|
E |
ceiling(E e)
|
Comparator |
comparator()
Returns the comparator that orders the elements, which is
Ordering when the natural ordering of the elements is used.
|
static <E> ImmutableSortedSet |
copyOf(Collection
Returns an immutable sorted set containing the given elements sorted by their natural ordering.
|
static <E> ImmutableSortedSet |
copyOf(Comparator
Returns an immutable sorted set containing the given elements sorted by the given
Comparator.
|
static <E> ImmutableSortedSet |
copyOf(Comparator
Returns an immutable sorted set containing the given elements sorted by the given
Comparator.
|
static <E> ImmutableSortedSet |
copyOf(Comparator
Returns an immutable sorted set containing the given elements sorted by the given
Comparator.
|
static <E extends Comparable |
copyOf(E[] elements)
Returns an immutable sorted set containing the given elements sorted by their natural ordering.
|
static <E> ImmutableSortedSet |
copyOf(Iterable
Returns an immutable sorted set containing the given elements sorted by their natural ordering.
|
static <E> ImmutableSortedSet |
copyOf(Iterator
Returns an immutable sorted set containing the given elements sorted by their natural ordering.
|
static <E> ImmutableSortedSet |
copyOfSorted(SortedSet
Returns an immutable sorted set containing the elements of a sorted set, sorted by the same
Comparator.
|
abstract UnmodifiableIterator |
descendingIterator()
|
ImmutableSortedSet |
descendingSet()
|
E |
first()
|
E |
floor(E e)
|
ImmutableSortedSet |
headSet(E toElement)
|
ImmutableSortedSet |
headSet(E toElement, boolean inclusive)
|
E |
higher(E e)
|
abstract UnmodifiableIterator |
iterator()
Returns an unmodifiable iterator across the elements in this collection.
|
E |
last()
|
E |
lower(E e)
|
static <E extends Comparable |
naturalOrder()
Returns a builder that creates immutable sorted sets whose elements are ordered by their natural ordering.
|
static <E> ImmutableSortedSet |
of()
Returns the empty immutable sorted set.
|
static <E extends Comparable |
of(E element)
Returns an immutable sorted set containing a single element.
|
static <E extends Comparable |
of(E e1, E e2)
Returns an immutable sorted set containing the given elements sorted by their natural ordering.
|
static <E extends Comparable |
of(E e1, E e2, E e3)
Returns an immutable sorted set containing the given elements sorted by their natural ordering.
|
static <E extends Comparable |
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 extends Comparable |
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 extends Comparable |
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 |
orderedBy(Comparator
Returns a builder that creates immutable sorted sets with an explicit comparator.
|
E |
pollFirst()
Deprecated.
Unsupported operation.
|
E |
pollLast()
Deprecated.
Unsupported operation.
|
static <E extends Comparable |
reverseOrder()
Returns a builder that creates immutable sorted sets whose elements are ordered by the reverse of their natural ordering.
|
ImmutableSortedSet |
subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
|
ImmutableSortedSet |
subSet(E fromElement, E toElement)
|
ImmutableSortedSet |
tailSet(E fromElement)
|
ImmutableSortedSet |
tailSet(E fromElement, boolean inclusive)
|
equals, hashCodeadd, addAll, asList, clear, contains, remove, removeAll, retainAll, toArray, toArraycontainsAll, isEmpty, size, toStringclone, finalize, getClass, notify, notifyAll, wait, wait, waitspliteratoradd, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, remove, removeAll, retainAll, size, toArray, toArrayparallelStream, removeIf, streampublic static <E> ImmutableSortedSet<E> of()
public static <E extends Comparable<? super E>> ImmutableSortedSet <E> of(E element)
public static <E extends Comparable<? super E>> ImmutableSortedSet <E> of(E e1, E e2)
Comparable.compareTo(T) , only the first one specified is included.
NullPointerException - if any element is null
public static <E extends Comparable<? super E>> ImmutableSortedSet <E> of(E e1, E e2, E e3)
Comparable.compareTo(T) , only the first one specified is included.
NullPointerException - if any element is null
public static <E extends Comparable<? super E>> ImmutableSortedSet <E> of(E e1, E e2, E e3, E e4)
Comparable.compareTo(T) , only the first one specified is included.
NullPointerException - if any element is null
public static <E extends Comparable<? super E>> ImmutableSortedSet <E> of(E e1, E e2, E e3, E e4, E e5)
Comparable.compareTo(T) , only the first one specified is included.
NullPointerException - if any element is null
public static <E extends Comparable<? super E>> ImmutableSortedSet <E> of(E e1, E e2, E e3, E e4, E e5, E e6, E... remaining)
Comparable.compareTo(T) , only the first one specified is included.
NullPointerException - if any element is null
public static <E extends Comparable<? super E>> ImmutableSortedSet <E> copyOf(E[] elements)
Comparable.compareTo(T) , only the first one specified is included.
NullPointerException - if any of
elements is null
public static <E> ImmutableSortedSet<E> copyOf(Iterable <? extends E> elements)
compareTo(), only the first one specified is included. To create a copy of a
SortedSet that preserves the comparator, call
copyOfSorted(java.util.SortedSet<E>) instead. This method iterates over
elements at most once.
Note that if s is a Set<String>, then ImmutableSortedSet.copyOf(s) returns an ImmutableSortedSet<String> containing each of the strings in s, while ImmutableSortedSet.of(s) returns an ImmutableSortedSet<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.
ClassCastException - if the elements are not mutually comparable
NullPointerException - if any of
elements is null
public static <E> ImmutableSortedSet<E> copyOf(Collection <? extends E> elements)
compareTo(), only the first one specified is included. To create a copy of a
SortedSet that preserves the comparator, call
copyOfSorted(java.util.SortedSet<E>) instead. This method iterates over
elements at most once.
Note that if s is a Set<String>, then ImmutableSortedSet.copyOf(s) returns an ImmutableSortedSet<String> containing each of the strings in s, while ImmutableSortedSet.of(s) returns an ImmutableSortedSet<Set<String>> containing one element (the given set itself).
Note: Despite what the method name suggests, if elements is an ImmutableSortedSet, 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 elements is a synchronized or concurrent collection that is currently being modified by another thread.
ClassCastException - if the elements are not mutually comparable
NullPointerException - if any of
elements is null
public static <E> ImmutableSortedSet<E> copyOf(Iterator <? extends E> elements)
compareTo(), 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.
ClassCastException - if the elements are not mutually comparable
NullPointerException - if any of
elements is null
public static <E> ImmutableSortedSet<E> copyOf(Comparator <? super E> comparator, Iterator <? extends E> elements)
Comparator. When multiple elements are equivalent according to
compareTo(), only the first one specified is included.
NullPointerException - if
comparator or any of
elements is null
public static <E> ImmutableSortedSet<E> copyOf(Comparator <? super E> comparator, Iterable <? extends E> elements)
Comparator. When multiple elements are equivalent according to
compare(), only the first one specified is included. This method iterates over
elements at 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.
NullPointerException - if
comparator or any of
elements is null
public static <E> ImmutableSortedSet<E> copyOf(Comparator <? super E> comparator, Collection <? extends E> elements)
Comparator. When multiple elements are equivalent according to
compareTo(), 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 elements is a synchronized or concurrent collection that is currently being modified by another thread.
NullPointerException - if
comparator or any of
elements is null
public static <E> ImmutableSortedSet<E> copyOfSorted(SortedSet <E> sortedSet)
Comparator. That behavior differs from
copyOf(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 sortedSet is a synchronized or concurrent collection that is currently being modified by another thread.
NullPointerException - if
sortedSet or any of its elements is null
public static <E> ImmutableSortedSet.Builder <E> orderedBy(Comparator <E> comparator)
SortedSet<Integer> with a
Comparator<Number>, use the
ImmutableSortedSet.Builder constructor instead.
NullPointerException - if
comparator is null
public static <E extends Comparable<?>> ImmutableSortedSet .Builder <E> reverseOrder()
public static <E extends Comparable<?>> ImmutableSortedSet .Builder <E> naturalOrder()
Ordering.natural() as the comparator. This method provides more type-safety than
builder(), as it can be called only for classes that implement
Comparable.
public Comparator<? super E> comparator()
Ordering.natural() when the natural ordering of the elements is used. Note that its behavior is not consistent with
SortedSet.comparator() , which returns
null to indicate natural ordering.
public abstract UnmodifiableIterator<E> iterator()
ImmutableCollection
public ImmutableSortedSet<E> headSet(E toElement)
This method returns a serializable ImmutableSortedSet.
The SortedSet documentation states that a subset of a subset throws an IllegalArgumentException if passed a toElement greater than an earlier toElement. However, this method doesn't throw an exception in that situation, but instead keeps the original toElement.
@GwtIncompatible(value="NavigableSet") public ImmutableSortedSet<E> headSet(E toElement, boolean inclusive)
headSet in interface
NavigableSet<E>
public ImmutableSortedSet<E> subSet(E fromElement, E toElement)
This method returns a serializable ImmutableSortedSet.
The SortedSet documentation states that a subset of a subset throws an IllegalArgumentException if passed a fromElement smaller than an earlier fromElement. However, this method doesn't throw an exception in that situation, but instead keeps the original fromElement. Similarly, this method keeps the original toElement, instead of throwing an exception, if passed a toElement greater than an earlier toElement.
@GwtIncompatible(value="NavigableSet") public ImmutableSortedSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
subSet in interface
NavigableSet<E>
public ImmutableSortedSet<E> tailSet(E fromElement)
This method returns a serializable ImmutableSortedSet.
The SortedSet documentation states that a subset of a subset throws an IllegalArgumentException if passed a fromElement smaller than an earlier fromElement. However, this method doesn't throw an exception in that situation, but instead keeps the original fromElement.
@GwtIncompatible(value="NavigableSet") public ImmutableSortedSet<E> tailSet(E fromElement, boolean inclusive)
tailSet in interface
NavigableSet<E>
@GwtIncompatible(value="NavigableSet") public E lower(E e)
lower in interface
NavigableSet<E>
@GwtIncompatible(value="NavigableSet") public E floor(E e)
floor in interface
NavigableSet<E>
@GwtIncompatible(value="NavigableSet") public E ceiling(E e)
ceiling in interface
NavigableSet<E>
@GwtIncompatible(value="NavigableSet") public E higher(E e)
higher in interface
NavigableSet<E>
public E first()
public E last()
@Deprecated @GwtIncompatible(value="NavigableSet") public final E pollFirst()
pollFirst in interface
NavigableSet<E>
UnsupportedOperationException - always
@Deprecated @GwtIncompatible(value="NavigableSet") public final E pollLast()
pollLast in interface
NavigableSet<E>
UnsupportedOperationException - always
@GwtIncompatible(value="NavigableSet") public ImmutableSortedSet<E> descendingSet()
descendingSet in interface
NavigableSet<E>
@GwtIncompatible(value="NavigableSet") public abstract UnmodifiableIterator<E> descendingIterator()
descendingIterator in interface
NavigableSet<E>
@Deprecated public static <E> ImmutableSortedSet.Builder <E> builder()
naturalOrder(), which offers better type-safety.
naturalOrder(), which offers better type-safety, instead. This method exists only to hide
ImmutableSet.builder() from consumers of
ImmutableSortedSet.
UnsupportedOperationException - always