@GwtCompatible(serializable=true, emulated=true) public abstract class ImmutableSortedSet<E> extends ImmutableSet<E> implements NavigableSet<E>
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.
NavigableSet
since 12.0)Modifier and Type | Class and Description |
---|---|
static class |
ImmutableSortedSet.Builder<E>
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<E> |
builder()
Deprecated.
Use
naturalOrder() , which offers better type-safety. |
static <E> ImmutableSortedSet.Builder<E> |
builderWithExpectedSize(int expectedSize)
Deprecated.
Not supported by ImmutableSortedSet.
|
E |
ceiling(E e)
Returns the least element in this set greater than or equal to
the given element, or
null if there is no such element. |
Comparator<? super E> |
comparator()
Returns the comparator that orders the elements, which is
Ordering.natural() when the
natural ordering of the elements is used. |
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,
Collection<? extends E> elements)
Returns an immutable sorted set containing the given elements sorted by the given
Comparator . |
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 given
Comparator . |
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 given
Comparator . |
static <E extends Comparable<? super E>> |
copyOf(E[] elements)
Returns an immutable sorted set containing the given elements sorted by their natural ordering.
|
static <E> ImmutableSortedSet<E> |
copyOf(Iterable<? extends E> elements)
Returns an immutable sorted set containing the given elements sorted by their natural ordering.
|
static <E> ImmutableSortedSet<E> |
copyOf(Iterator<? extends E> elements)
Returns an immutable sorted set containing the given elements sorted by their natural ordering.
|
static <E> ImmutableSortedSet<E> |
copyOfSorted(SortedSet<E> sortedSet)
Returns an immutable sorted set containing the elements of a sorted set, sorted by the same
Comparator . |
abstract UnmodifiableIterator<E> |
descendingIterator()
Returns an iterator over the elements in this set, in descending order.
|
ImmutableSortedSet<E> |
descendingSet()
Returns a reverse order view of the elements contained in this set.
|
E |
first()
Returns the first (lowest) element currently in this set.
|
E |
floor(E e)
Returns the greatest element in this set less than or equal to
the given element, or
null if there is no such element. |
ImmutableSortedSet<E> |
headSet(E toElement)
Returns a view of the portion of this set whose elements are
strictly less than toElement.
|
ImmutableSortedSet<E> |
headSet(E toElement,
boolean inclusive)
Returns a view of the portion of this set whose elements are less than
(or equal to, if
inclusive is true) toElement . |
E |
higher(E e)
Returns the least element in this set strictly greater than the
given element, or
null if there is no such element. |
abstract UnmodifiableIterator<E> |
iterator()
Returns an unmodifiable iterator across the elements in this collection.
|
E |
last()
Returns the last (highest) element currently in this set.
|
E |
lower(E e)
Returns the greatest element in this set strictly less than the
given element, or
null if there is no such element. |
static <E extends Comparable<?>> |
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 Comparable<? super E>> |
of(E element)
Returns an immutable sorted set containing a single element.
|
static <E extends Comparable<? super E>> |
of(E e1,
E e2)
Returns an immutable sorted set containing the given elements sorted by their natural ordering.
|
static <E extends Comparable<? super E>> |
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<? super 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 extends Comparable<? super 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 extends Comparable<? super 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.Builder<E> |
orderedBy(Comparator<E> 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.
|
Spliterator<E> |
spliterator()
Creates a
Spliterator over the elements in this collection. |
ImmutableSortedSet<E> |
subSet(E fromElement,
boolean fromInclusive,
E toElement,
boolean toInclusive)
Returns a view of the portion of this set whose elements range from
fromElement to toElement . |
ImmutableSortedSet<E> |
subSet(E fromElement,
E toElement)
Returns a view of the portion of this set whose elements range
from fromElement, inclusive, to toElement,
exclusive.
|
ImmutableSortedSet<E> |
tailSet(E fromElement)
Returns a view of the portion of this set whose elements are
greater than or equal to fromElement.
|
ImmutableSortedSet<E> |
tailSet(E fromElement,
boolean inclusive)
Returns a view of the portion of this set whose elements are greater
than (or equal to, if
inclusive is true) fromElement . |
static <E> Collector<E,?,ImmutableSet<E>> |
toImmutableSet()
Deprecated.
|
static <E> Collector<E,?,ImmutableSortedSet<E>> |
toImmutableSortedSet(Comparator<? super E> comparator)
Returns a
Collector that accumulates the input elements into a new ImmutableSortedSet , ordered by the specified comparator. |
asList, equals, hashCode
add, addAll, clear, contains, remove, removeAll, removeIf, retainAll, toArray, toArray
containsAll, isEmpty, size, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, remove, removeAll, retainAll, size, toArray, toArray
parallelStream, removeIf, stream
@Beta public static <E> Collector<E,?,ImmutableSortedSet<E>> toImmutableSortedSet(Comparator<? super E> comparator)
Collector
that accumulates the input elements into a new ImmutableSortedSet
, 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.
public 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 nullpublic 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 nullpublic 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 nullpublic 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 nullpublic 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 nullpublic 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 nullpublic 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 comparableNullPointerException
- if any of elements
is nullpublic 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 comparableNullPointerException
- if any of elements
is nullpublic 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 comparableNullPointerException
- if any of elements
is nullpublic 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 nullpublic 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 nullpublic 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 nullpublic 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 nullpublic 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 nullpublic 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.comparator
in interface SortedSet<E>
public abstract UnmodifiableIterator<E> iterator()
ImmutableCollection
iterator
in interface Iterable<E>
iterator
in interface Collection<E>
iterator
in interface NavigableSet<E>
iterator
in interface Set<E>
iterator
in class ImmutableSet<E>
public ImmutableSortedSet<E> headSet(E toElement)
The returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range.
Equivalent to headSet(toElement, false)
.
This method returns a serializable ImmutableSortedSet
.
The SortedSet.headSet(E)
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 public ImmutableSortedSet<E> headSet(E toElement, boolean inclusive)
java.util.NavigableSet
inclusive
is true) toElement
. The
returned set is backed by this set, so changes in the returned set are
reflected in this set, and vice-versa. The returned set supports all
optional set operations that this set supports.
The returned set will throw an IllegalArgumentException
on an attempt to insert an element outside its range.
headSet
in interface NavigableSet<E>
toElement
- high endpoint of the returned setinclusive
- true
if the high endpoint
is to be included in the returned viewinclusive
is true) toElement
public ImmutableSortedSet<E> subSet(E fromElement, E toElement)
The returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range.
Equivalent to subSet(fromElement, true, toElement, false)
.
This method returns a serializable ImmutableSortedSet
.
The SortedSet.subSet(E, E)
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
.
subSet
in interface NavigableSet<E>
subSet
in interface SortedSet<E>
fromElement
- low endpoint (inclusive) of the returned settoElement
- high endpoint (exclusive) of the returned set@GwtIncompatible public ImmutableSortedSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
java.util.NavigableSet
fromElement
to toElement
. If fromElement
and
toElement
are equal, the returned set is empty unless fromInclusive
and toInclusive
are both true. The returned set
is backed by this set, so changes in the returned set are reflected in
this set, and vice-versa. The returned set supports all optional set
operations that this set supports.
The returned set will throw an IllegalArgumentException
on an attempt to insert an element outside its range.
subSet
in interface NavigableSet<E>
fromElement
- low endpoint of the returned setfromInclusive
- true
if the low endpoint
is to be included in the returned viewtoElement
- high endpoint of the returned settoInclusive
- true
if the high endpoint
is to be included in the returned viewfromElement
, inclusive, to toElement
, exclusivepublic ImmutableSortedSet<E> tailSet(E fromElement)
The returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range.
Equivalent to tailSet(fromElement, true)
.
This method returns a serializable ImmutableSortedSet
.
The SortedSet.tailSet(E)
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 public ImmutableSortedSet<E> tailSet(E fromElement, boolean inclusive)
java.util.NavigableSet
inclusive
is true) fromElement
.
The returned set is backed by this set, so changes in the returned set
are reflected in this set, and vice-versa. The returned set supports
all optional set operations that this set supports.
The returned set will throw an IllegalArgumentException
on an attempt to insert an element outside its range.
tailSet
in interface NavigableSet<E>
fromElement
- low endpoint of the returned setinclusive
- true
if the low endpoint
is to be included in the returned viewfromElement
@GwtIncompatible public E lower(E e)
java.util.NavigableSet
null
if there is no such element.lower
in interface NavigableSet<E>
e
- the value to matche
,
or null
if there is no such element@GwtIncompatible public E floor(E e)
java.util.NavigableSet
null
if there is no such element.floor
in interface NavigableSet<E>
e
- the value to matche
,
or null
if there is no such element@GwtIncompatible public E ceiling(E e)
java.util.NavigableSet
null
if there is no such element.ceiling
in interface NavigableSet<E>
e
- the value to matche
,
or null
if there is no such element@GwtIncompatible public E higher(E e)
java.util.NavigableSet
null
if there is no such element.higher
in interface NavigableSet<E>
e
- the value to matche
,
or null
if there is no such elementpublic E first()
java.util.SortedSet
public E last()
java.util.SortedSet
@CanIgnoreReturnValue @Deprecated @GwtIncompatible public final E pollFirst()
pollFirst
in interface NavigableSet<E>
null
if this set is emptyUnsupportedOperationException
- always@CanIgnoreReturnValue @Deprecated @GwtIncompatible public final E pollLast()
pollLast
in interface NavigableSet<E>
null
if this set is emptyUnsupportedOperationException
- always@GwtIncompatible public ImmutableSortedSet<E> descendingSet()
java.util.NavigableSet
remove
operation), the results of
the iteration are undefined.
The returned set has an ordering equivalent to
Collections.reverseOrder
(comparator()).
The expression s.descendingSet().descendingSet()
returns a
view of s
essentially equivalent to s
.
descendingSet
in interface NavigableSet<E>
public Spliterator<E> spliterator()
java.util.Collection
Spliterator
over the elements in this collection.
Implementations should document characteristic values reported by the
spliterator. Such characteristic values are not required to be reported
if the spliterator reports Spliterator.SIZED
and this collection
contains no elements.
The default implementation should be overridden by subclasses that
can return a more efficient spliterator. In order to
preserve expected laziness behavior for the Collection.stream()
and
Collection.parallelStream()
} methods, spliterators should either have the
characteristic of IMMUTABLE
or CONCURRENT
, or be
late-binding.
If none of these is practical, the overriding class should describe the
spliterator's documented policy of binding and structural interference,
and should override the Collection.stream()
and Collection.parallelStream()
methods to create streams using a Supplier
of the spliterator,
as in:
Stream<E> s = StreamSupport.stream(() -> spliterator(), spliteratorCharacteristics)
These requirements ensure that streams produced by the
Collection.stream()
and Collection.parallelStream()
methods will reflect the
contents of the collection as of initiation of the terminal stream
operation.
spliterator
in interface Iterable<E>
spliterator
in interface Collection<E>
spliterator
in interface Set<E>
spliterator
in interface SortedSet<E>
spliterator
in class ImmutableCollection<E>
Spliterator
over the elements in this collection@GwtIncompatible public abstract UnmodifiableIterator<E> descendingIterator()
java.util.NavigableSet
descendingSet().iterator()
.descendingIterator
in interface NavigableSet<E>
@Deprecated public static <E> Collector<E,?,ImmutableSet<E>> toImmutableSet()
toImmutableSortedSet(java.util.Comparator<? super E>)
.toImmutableSortedSet(java.util.Comparator<? super E>)
instead. This method exists
only to hide ImmutableSet.toImmutableSet()
from consumers of ImmutableSortedSet
.UnsupportedOperationException
- always@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@Deprecated public static <E> ImmutableSortedSet.Builder<E> builderWithExpectedSize(int expectedSize)
ImmutableSet.builderWithExpectedSize(int)
from consumers of ImmutableSortedSet
.UnsupportedOperationException
- alwaysCopyright © 2010–2017. All rights reserved.