Class ImmutableSet<E>

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.Set<E>
    Direct Known Subclasses:
    ImmutableSortedSet

    @GwtCompatible(serializable=true,
                   emulated=true)
    public abstract class ImmutableSet<E>
    extends ImmutableCollection<E>
    implements java.util.Set<E>
    A high-performance, immutable Set with reliable, user-specified iteration order. Does not permit null elements.

    Unlike Collections.unmodifiableSet(java.util.Set<? extends T>), which is a view of a separate collection that can still change, an instance of this class 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.

    Warning: Like most sets, an ImmutableSet 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.

    This class has been observed to perform significantly better than HashSet for objects with very fast Object.hashCode() implementations (as a well-behaved immutable object should). While this class's factory methods create hash-based instances, the ImmutableSortedSet subclass performs binary searches instead.

    Note: Although this class is not final, it cannot be subclassed outside its package 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)
    See Also:
    ImmutableList, ImmutableMap, Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  ImmutableSet.Builder<E>
      A builder for creating immutable set instances, especially public static final sets ("constant sets").
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      static <E> ImmutableSet.Builder<E> builder()
      Returns a new builder.
      static <E> ImmutableSet<E> copyOf​(E[] elements)
      Returns an immutable set containing the given elements, in order.
      static <E> ImmutableSet<E> copyOf​(java.lang.Iterable<? extends E> elements)
      Returns an immutable set containing the given elements, in order.
      static <E> ImmutableSet<E> copyOf​(java.util.Collection<? extends E> elements)
      Returns an immutable set containing the given elements, in order.
      static <E> ImmutableSet<E> copyOf​(java.util.Iterator<? extends E> elements)
      Returns an immutable set containing the given elements, in order.
      boolean equals​(java.lang.Object object)  
      int hashCode()  
      abstract UnmodifiableIterator<E> iterator()
      Returns an unmodifiable iterator across the elements in this collection.
      static <E> ImmutableSet<E> of()
      Returns the empty immutable set.
      static <E> ImmutableSet<E> of​(E element)
      Returns an immutable set containing a single element.
      static <E> ImmutableSet<E> of​(E e1, E e2)
      Returns an immutable set containing the given elements, in order.
      static <E> ImmutableSet<E> of​(E e1, E e2, E e3)
      Returns an immutable set containing the given elements, in order.
      static <E> ImmutableSet<E> of​(E e1, E e2, E e3, E e4)
      Returns an immutable set containing the given elements, in order.
      static <E> ImmutableSet<E> of​(E e1, E e2, E e3, E e4, E e5)
      Returns an immutable set containing the given elements, in order.
      static <E> ImmutableSet<E> of​(E e1, E e2, E e3, E e4, E e5, E e6, E... others)
      Returns an immutable set containing the given elements, in order.
      • Methods inherited from class java.util.AbstractCollection

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

        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, isEmpty, remove, removeAll, retainAll, size, spliterator, toArray, toArray
    • Method Detail

      • of

        public static <E> ImmutableSet<E> of()
        Returns the empty immutable set. This set behaves and performs comparably to Collections.emptySet(), and is preferable mainly for consistency and maintainability of your code.
      • of

        public static <E> ImmutableSet<E> of​(E element)
        Returns an immutable set containing a single element. This set behaves and performs comparably to Collections.singleton(T), but will not accept a null element. It is preferable mainly for consistency and maintainability of your code.
      • of

        public static <E> ImmutableSet<E> of​(E e1,
                                             E e2)
        Returns an immutable set containing the given elements, in order. Repeated occurrences of an element (according to Object.equals(java.lang.Object)) after the first are ignored.
        Throws:
        java.lang.NullPointerException - if any element is null
      • of

        public static <E> ImmutableSet<E> of​(E e1,
                                             E e2,
                                             E e3)
        Returns an immutable set containing the given elements, in order. Repeated occurrences of an element (according to Object.equals(java.lang.Object)) after the first are ignored.
        Throws:
        java.lang.NullPointerException - if any element is null
      • of

        public static <E> ImmutableSet<E> of​(E e1,
                                             E e2,
                                             E e3,
                                             E e4)
        Returns an immutable set containing the given elements, in order. Repeated occurrences of an element (according to Object.equals(java.lang.Object)) after the first are ignored.
        Throws:
        java.lang.NullPointerException - if any element is null
      • of

        public static <E> ImmutableSet<E> of​(E e1,
                                             E e2,
                                             E e3,
                                             E e4,
                                             E e5)
        Returns an immutable set containing the given elements, in order. Repeated occurrences of an element (according to Object.equals(java.lang.Object)) after the first are ignored.
        Throws:
        java.lang.NullPointerException - if any element is null
      • of

        public static <E> ImmutableSet<E> of​(E e1,
                                             E e2,
                                             E e3,
                                             E e4,
                                             E e5,
                                             E e6,
                                             E... others)
        Returns an immutable set containing the given elements, in order. Repeated occurrences of an element (according to Object.equals(java.lang.Object)) after the first are ignored.
        Throws:
        java.lang.NullPointerException - if any element is null
        Since:
        3.0 (source-compatible since 2.0)
      • copyOf

        public static <E> ImmutableSet<E> copyOf​(E[] elements)
        Returns an immutable set containing the given elements, in order. Repeated occurrences of an element (according to Object.equals(java.lang.Object)) after the first are ignored.
        Throws:
        java.lang.NullPointerException - if any of elements is null
        Since:
        3.0
      • copyOf

        public static <E> ImmutableSet<E> copyOf​(java.lang.Iterable<? extends E> elements)
        Returns an immutable set containing the given elements, in order. Repeated occurrences of an element (according to Object.equals(java.lang.Object)) after the first are ignored. This method iterates over elements at most once.

        Note that if s is a Set<String>, then ImmutableSet.copyOf(s) returns an ImmutableSet<String> containing each of the strings in s, while ImmutableSet.of(s) returns a ImmutableSet<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.

        Throws:
        java.lang.NullPointerException - if any of elements is null
      • copyOf

        public static <E> ImmutableSet<E> copyOf​(java.util.Iterator<? extends E> elements)
        Returns an immutable set containing the given elements, in order. Repeated occurrences of an element (according to Object.equals(java.lang.Object)) after the first are ignored.
        Throws:
        java.lang.NullPointerException - if any of elements is null
      • copyOf

        public static <E> ImmutableSet<E> copyOf​(java.util.Collection<? extends E> elements)
        Returns an immutable set containing the given elements, in order. Repeated occurrences of an element (according to Object.equals(java.lang.Object)) after the first are ignored. This method iterates over elements at most once.

        Note that if s is a Set<String>, then ImmutableSet.copyOf(s) returns an ImmutableSet<String> containing each of the strings in s, while ImmutableSet.of(s) returns a ImmutableSet<Set<String>> containing one element (the given set itself).

        Note: Despite what the method name suggests, copyOf will return constant-space views, rather than linear-space copies, of some inputs known to be immutable. For some other immutable inputs, such as key sets of an ImmutableMap, it still performs a copy in order to avoid holding references to the values of the map. The heuristics used in this decision are undocumented and subject to change except that:

        • A full copy will be done of any ImmutableSortedSet.
        • ImmutableSet.copyOf() is idempotent with respect to pointer equality.

        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 any of elements is null
        Since:
        7.0 (source-compatible since 2.0)
      • equals

        public boolean equals​(@Nullable
                              java.lang.Object object)
        Specified by:
        equals in interface java.util.Collection<E>
        Specified by:
        equals in interface java.util.Set<E>
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Collection<E>
        Specified by:
        hashCode in interface java.util.Set<E>
        Overrides:
        hashCode in class java.lang.Object
      • iterator

        public abstract UnmodifiableIterator<E> iterator()
        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.Set<E>
        Specified by:
        iterator in class ImmutableCollection<E>