Class 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>
    A 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 NavigableSet since 12.0)
    Author:
    Jared Levy, Louis Wasserman
    See Also:
    Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class 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.
    • 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.
      Use naturalOrder(), which offers better type-safety.
      static <E> ImmutableSortedSet.Builder<E> builderWithExpectedSize​(int expectedSize)
      Deprecated.
      Not supported by ImmutableSortedSet.
      E ceiling​(E e)  
      java.util.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 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​(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 given Comparator.
      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 given Comparator.
      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 given Comparator.
      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 <Z> ImmutableSortedSet<Z> copyOf​(Z[] elements)
      Deprecated.
      Pass parameters of type Comparable to use copyOf(Comparable[]).
      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 same Comparator.
      abstract UnmodifiableIterator<E> descendingIterator()  
      ImmutableSortedSet<E> descendingSet()  
      E first()  
      E floor​(E e)  
      ImmutableSortedSet<E> headSet​(E toElement)
      ImmutableSortedSet<E> headSet​(E toElement, boolean inclusive)  
      E higher​(E e)  
      abstract UnmodifiableIterator<E> iterator()
      Returns an unmodifiable iterator across the elements in this collection.
      E last()  
      E lower​(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 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 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 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 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 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.Builder<E> orderedBy​(java.util.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 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 java.util.AbstractCollection

        containsAll, isEmpty, size, toString
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, removeIf, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.Set

        add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, remove, removeAll, retainAll, size, toArray, toArray
      • Methods inherited from interface java.util.SortedSet

        spliterator
    • Method Detail

      • of

        public static <E> ImmutableSortedSet<E> of()
        Returns the empty immutable sorted set.

        Performance note: the instance returned is a singleton.

      • 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 to Comparable.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 to Comparable.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 to Comparable.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 to Comparable.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 to Comparable.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 to Comparable.compareTo(T), only the first one specified is included.
        Throws:
        java.lang.NullPointerException - if any of elements is 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 to 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.

        Throws:
        java.lang.ClassCastException - if the elements are not mutually comparable
        java.lang.NullPointerException - if any of elements is 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 to 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.

        Throws:
        java.lang.ClassCastException - if the elements are not mutually comparable
        java.lang.NullPointerException - if any of elements is 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 to 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.

        Throws:
        java.lang.ClassCastException - if the elements are not mutually comparable
        java.lang.NullPointerException - if any of elements is 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 given Comparator. When multiple elements are equivalent according to compareTo(), only the first one specified is included.
        Throws:
        java.lang.NullPointerException - if comparator or any of elements is 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 given 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.

        Throws:
        java.lang.NullPointerException - if comparator or any of elements is 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 given 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.

        Throws:
        java.lang.NullPointerException - if comparator or any of elements is 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 same 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.

        Throws:
        java.lang.NullPointerException - if sortedSet or 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 a SortedSet<Integer> with a Comparator<Number>, use the ImmutableSortedSet.Builder constructor instead.
        Throws:
        java.lang.NullPointerException - if comparator is 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 use Ordering.natural() as the comparator. This method provides more type-safety than builder(), as it can be called only for classes that implement Comparable.
      • comparator

        public java.util.Comparator<? super Ecomparator()
        Returns the comparator that orders the elements, which is 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.
        Specified by:
        comparator in interface java.util.SortedSet<E>
      • iterator

        public abstract UnmodifiableIterator<Eiterator()
        Description copied from class: ImmutableCollection
        Returns an unmodifiable iterator across the elements in this collection.
        Specified by:
        iterator in interface java.util.Collection<E>
        Specified by:
        iterator in interface java.lang.Iterable<E>
        Specified by:
        iterator in interface java.util.NavigableSet<E>
        Specified by:
        iterator in interface java.util.Set<E>
        Specified by:
        iterator in class ImmutableSet<E>
      • headSet

        public ImmutableSortedSet<EheadSet​(E toElement)

        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.

        Specified by:
        headSet in interface java.util.NavigableSet<E>
        Specified by:
        headSet in interface java.util.SortedSet<E>
      • headSet

        public ImmutableSortedSet<EheadSet​(E toElement,
                                             boolean inclusive)
        Specified by:
        headSet in interface java.util.NavigableSet<E>
        Since:
        12.0
      • subSet

        public ImmutableSortedSet<EsubSet​(E fromElement,
                                            E toElement)

        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.

        Specified by:
        subSet in interface java.util.NavigableSet<E>
        Specified by:
        subSet in interface java.util.SortedSet<E>
      • tailSet

        public ImmutableSortedSet<EtailSet​(E fromElement)

        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.

        Specified by:
        tailSet in interface java.util.NavigableSet<E>
        Specified by:
        tailSet in interface java.util.SortedSet<E>
      • tailSet

        public ImmutableSortedSet<EtailSet​(E fromElement,
                                             boolean inclusive)
        Specified by:
        tailSet in interface java.util.NavigableSet<E>
        Since:
        12.0
      • floor

        @CheckForNull
        public E floor​(E e)
        Specified by:
        floor in interface java.util.NavigableSet<E>
        Since:
        12.0
      • ceiling

        @CheckForNull
        public E ceiling​(E e)
        Specified by:
        ceiling in interface java.util.NavigableSet<E>
        Since:
        12.0
      • first

        public E first()
        Specified by:
        first in interface java.util.SortedSet<E>
      • last

        public E last()
        Specified by:
        last in interface java.util.SortedSet<E>
      • pollFirst

        @CanIgnoreReturnValue
        @Deprecated
        @GwtIncompatible
        @CheckForNull
        public final E pollFirst()
        Deprecated.
        Unsupported operation.
        Guaranteed to throw an exception and leave the set unmodified.
        Specified by:
        pollFirst in interface java.util.NavigableSet<E>
        Throws:
        java.lang.UnsupportedOperationException - always
        Since:
        12.0
      • pollLast

        @CanIgnoreReturnValue
        @Deprecated
        @GwtIncompatible
        @CheckForNull
        public final E pollLast()
        Deprecated.
        Unsupported operation.
        Guaranteed to throw an exception and leave the set unmodified.
        Specified by:
        pollLast in interface java.util.NavigableSet<E>
        Throws:
        java.lang.UnsupportedOperationException - always
        Since:
        12.0
      • of

        @Deprecated
        public static <E> ImmutableSortedSet<E> of​(E element)
        Deprecated.
        Pass a parameter of type Comparable to use of(Comparable).
        Not supported. You are attempting to create a set that may contain a non-Comparable element. Proper calls will resolve to the version in ImmutableSortedSet, 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 type Comparable to use of(Comparable, Comparable).
        Not supported. You are attempting to create a set that may contain a non-Comparable element. Proper calls will resolve to the version in ImmutableSortedSet, 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 type Comparable to use of(Comparable, Comparable, Comparable).
        Not supported. You are attempting to create a set that may contain a non-Comparable element. Proper calls will resolve to the version in ImmutableSortedSet, 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 type Comparable to use of(Comparable, Comparable, Comparable, Comparable).
        Not supported. You are attempting to create a set that may contain a non-Comparable element. Proper calls will resolve to the version in ImmutableSortedSet, 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 type Comparable to use of( Comparable, Comparable, Comparable, Comparable, Comparable).
        Not supported. You are attempting to create a set that may contain a non-Comparable element. Proper calls will resolve to the version in ImmutableSortedSet, not this dummy version.
        Throws:
        java.lang.UnsupportedOperationException - always
      • copyOf

        @Deprecated
        public static <Z> ImmutableSortedSet<Z> copyOf​(Z[] elements)
        Deprecated.
        Pass parameters of type Comparable to use copyOf(Comparable[]).
        Not supported. You are attempting to create a set that may contain non-Comparable elements. Proper calls will resolve to the version in ImmutableSortedSet, not this dummy version.
        Throws:
        java.lang.UnsupportedOperationException - always