Class ImmutableList<E>

All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, List<E>, RandomAccess, SequencedCollection<E>

@GwtCompatible(serializable=true, emulated=true) public abstract class ImmutableList<E> extends ImmutableCollection<E> implements List<E>, RandomAccess
A List whose contents will never change, with many other important properties detailed at ImmutableCollection.

See the Guava User Guide article on immutable collections.

Since:
2.0
Author:
Kevin Bourrillion
See Also:
  • Method Details

    • toImmutableList

      public static <E> Collector<E,?,ImmutableList<E>> toImmutableList()
      Returns a Collector that accumulates the input elements into a new ImmutableList, in encounter order.
      Since:
      21.0
    • of

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

      Performance note: the instance returned is a singleton.

    • of

      public static <E> ImmutableList<E> of(E e1)
      Returns an immutable list containing a single element. This list behaves and performs comparably to Collections.singletonList(T), but will not accept a null element. It is preferable mainly for consistency and maintainability of your code.
      Throws:
      NullPointerException - if the element is null
    • of

      public static <E> ImmutableList<E> of(E e1, E e2)
      Returns an immutable list containing the given elements, in order.
      Throws:
      NullPointerException - if any element is null
    • of

      public static <E> ImmutableList<E> of(E e1, E e2, E e3)
      Returns an immutable list containing the given elements, in order.
      Throws:
      NullPointerException - if any element is null
    • of

      public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4)
      Returns an immutable list containing the given elements, in order.
      Throws:
      NullPointerException - if any element is null
    • of

      public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5)
      Returns an immutable list containing the given elements, in order.
      Throws:
      NullPointerException - if any element is null
    • of

      public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5, E e6)
      Returns an immutable list containing the given elements, in order.
      Throws:
      NullPointerException - if any element is null
    • of

      public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7)
      Returns an immutable list containing the given elements, in order.
      Throws:
      NullPointerException - if any element is null
    • of

      public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)
      Returns an immutable list containing the given elements, in order.
      Throws:
      NullPointerException - if any element is null
    • of

      public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9)
      Returns an immutable list containing the given elements, in order.
      Throws:
      NullPointerException - if any element is null
    • of

      public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10)
      Returns an immutable list containing the given elements, in order.
      Throws:
      NullPointerException - if any element is null
    • of

      public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10, E e11)
      Returns an immutable list containing the given elements, in order.
      Throws:
      NullPointerException - if any element is null
    • of

      @SafeVarargs public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10, E e11, E e12, E... others)
      Returns an immutable list containing the given elements, in order.

      The array others must not be longer than Integer.MAX_VALUE - 12.

      Throws:
      NullPointerException - if any element is null
      Since:
      3.0 (source-compatible since 2.0)
    • copyOf

      public static <E> ImmutableList<E> copyOf(Iterable<? extends E> elements)
      Returns an immutable list containing the given elements, in order. If elements is a Collection, this method behaves exactly as copyOf(Collection); otherwise, it behaves exactly as copyOf(elements.iterator().
      Throws:
      NullPointerException - if elements contains a null element
    • copyOf

      public static <E> ImmutableList<E> copyOf(Collection<? extends E> elements)
      Returns an immutable list containing the given elements, in order.

      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.

      Note that if list is a List<String>, then ImmutableList.copyOf(list) returns an ImmutableList<String> containing each of the strings in list, while ImmutableList.of(list) returns an ImmutableList<List<String>> containing one element (the given list itself).

      This method is safe to use even when elements is a synchronized or concurrent collection that is currently being modified by another thread.

      Throws:
      NullPointerException - if elements contains a null element
    • copyOf

      public static <E> ImmutableList<E> copyOf(Iterator<? extends E> elements)
      Returns an immutable list containing the given elements, in order.
      Throws:
      NullPointerException - if elements contains a null element
    • copyOf

      public static <E> ImmutableList<E> copyOf(E[] elements)
      Returns an immutable list containing the given elements, in order.
      Throws:
      NullPointerException - if elements contains a null element
      Since:
      3.0
    • sortedCopyOf

      public static <E extends Comparable<? super E>> ImmutableList<E> sortedCopyOf(Iterable<? extends E> elements)
      Returns an immutable list containing the given elements, sorted according to their natural order. The sorting algorithm used is stable, so elements that compare as equal will stay in the order in which they appear in the input.

      If your data has no duplicates, or you wish to deduplicate elements, use ImmutableSortedSet.copyOf(elements); if you want a List you can use its asList() view.

      Java 8+ users: If you want to convert a Stream to a sorted ImmutableList, use stream.sorted().collect(toImmutableList()).

      Throws:
      NullPointerException - if any element in the input is null
      Since:
      21.0
    • sortedCopyOf

      public static <E> ImmutableList<E> sortedCopyOf(Comparator<? super E> comparator, Iterable<? extends E> elements)
      Returns an immutable list containing the given elements, in sorted order relative to the specified comparator. The sorting algorithm used is stable, so elements that compare as equal will stay in the order in which they appear in the input.

      If your data has no duplicates, or you wish to deduplicate elements, use ImmutableSortedSet.copyOf(comparator, elements); if you want a List you can use its asList() view.

      Java 8+ users: If you want to convert a Stream to a sorted ImmutableList, use stream.sorted(comparator).collect(toImmutableList()).

      Throws:
      NullPointerException - if any element in the input is null
      Since:
      21.0
    • iterator

      Description copied from class: ImmutableCollection
      Returns an unmodifiable iterator across the elements in this collection.
      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface List<E>
      Specified by:
      iterator in class ImmutableCollection<E>
    • listIterator

      Specified by:
      listIterator in interface List<E>
    • listIterator

      Specified by:
      listIterator in interface List<E>
    • forEach

      public void forEach(Consumer<? super E> consumer)
      Specified by:
      forEach in interface Iterable<E>
    • indexOf

      public int indexOf(@Nullable Object object)
      Specified by:
      indexOf in interface List<E>
    • lastIndexOf

      public int lastIndexOf(@Nullable Object object)
      Specified by:
      lastIndexOf in interface List<E>
    • contains

      public boolean contains(@Nullable Object object)
      Specified by:
      contains in interface Collection<E>
      Specified by:
      contains in interface List<E>
      Specified by:
      contains in class ImmutableCollection<E>
    • subList

      public ImmutableList<E> subList(int fromIndex, int toIndex)
      Returns an immutable list of the elements between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the empty immutable list is returned.)

      Note: in almost all circumstances, the returned ImmutableList retains a strong reference to this, which may prevent the original list from being garbage collected. If you want the original list to be eligible for garbage collection, you should create and use a copy of the sub list (e.g., ImmutableList.copyOf(originalList.subList(...))).

      Specified by:
      subList in interface List<E>
    • addAll

      @CanIgnoreReturnValue @Deprecated public final boolean addAll(int index, Collection<? extends E> newElements)
      Deprecated.
      Unsupported operation.
      Guaranteed to throw an exception and leave the list unmodified.
      Specified by:
      addAll in interface List<E>
      Throws:
      UnsupportedOperationException - always
    • set

      @CanIgnoreReturnValue @Deprecated public final E set(int index, E element)
      Deprecated.
      Unsupported operation.
      Guaranteed to throw an exception and leave the list unmodified.
      Specified by:
      set in interface List<E>
      Throws:
      UnsupportedOperationException - always
    • add

      @Deprecated public final void add(int index, E element)
      Deprecated.
      Unsupported operation.
      Guaranteed to throw an exception and leave the list unmodified.
      Specified by:
      add in interface List<E>
      Throws:
      UnsupportedOperationException - always
    • remove

      @CanIgnoreReturnValue @Deprecated public final E remove(int index)
      Deprecated.
      Unsupported operation.
      Guaranteed to throw an exception and leave the list unmodified.
      Specified by:
      remove in interface List<E>
      Throws:
      UnsupportedOperationException - always
    • replaceAll

      @Deprecated public final void replaceAll(UnaryOperator<E> operator)
      Deprecated.
      Unsupported operation.
      Guaranteed to throw an exception and leave the list unmodified.
      Specified by:
      replaceAll in interface List<E>
      Throws:
      UnsupportedOperationException - always
    • sort

      @Deprecated public final void sort(@Nullable Comparator<? super E> c)
      Deprecated.
      Unsupported operation.
      Guaranteed to throw an exception and leave the list unmodified.
      Specified by:
      sort in interface List<E>
      Throws:
      UnsupportedOperationException - always
    • asList

      Deprecated.
      There is no reason to use this; it always returns this.
      Returns this list instance.
      Overrides:
      asList in class ImmutableCollection<E>
      Since:
      2.0
    • spliterator

      Specified by:
      spliterator in interface Collection<E>
      Specified by:
      spliterator in interface Iterable<E>
      Specified by:
      spliterator in interface List<E>
      Overrides:
      spliterator in class ImmutableCollection<E>
    • reverse

      public ImmutableList<E> reverse()
      Returns a view of this immutable list in reverse order. For example, ImmutableList.of(1, 2, 3).reverse() is equivalent to ImmutableList.of(3, 2, 1).
      Returns:
      a view of this immutable list in reverse order
      Since:
      7.0
    • equals

      public boolean equals(@Nullable Object obj)
      Specified by:
      equals in interface Collection<E>
      Specified by:
      equals in interface List<E>
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Collection<E>
      Specified by:
      hashCode in interface List<E>
      Overrides:
      hashCode in class Object
    • builder

      public static <E> ImmutableList.Builder<E> builder()
      Returns a new builder. The generated builder is equivalent to the builder created by the ImmutableList.Builder constructor.
    • builderWithExpectedSize

      public static <E> ImmutableList.Builder<E> builderWithExpectedSize(int expectedSize)
      Returns a new builder, expecting the specified number of elements to be added.

      If expectedSize is exactly the number of elements added to the builder before ImmutableList.Builder.build() is called, the builder is likely to perform better than an unsized builder() would have.

      It is not specified if any performance benefits apply if expectedSize is close to, but not exactly, the number of elements added to the builder.

      Since:
      23.1