Class ObjectImmutableList<K>

java.lang.Object
java.util.AbstractCollection<K>
All Implemented Interfaces:
ObjectCollection<K>, ObjectIterable<K>, ObjectList<K>, Stack<K>, java.io.Serializable, java.lang.Cloneable, java.lang.Comparable<java.util.List<? extends K>>, java.lang.Iterable<K>, java.util.Collection<K>, java.util.List<K>, java.util.RandomAccess

public class ObjectImmutableList<K>
extends AbstractObjectList<K>
implements ObjectList<K>, java.util.RandomAccess, java.lang.Cloneable, java.io.Serializable
A type-specific array-based immutable list; provides some additional methods that use polymorphism to avoid (un)boxing.

Instances of this class are immutable and (contrarily to mutable array-based list implementations) the backing array is not exposed. Instances can be built using a variety of methods, but note that constructors using an array will not make a defensive copy.

This class implements the bulk method getElements() using high-performance system calls (e.g., System.arraycopy()) instead of expensive loops.

See Also:
Serialized Form
  • Nested Class Summary

    Nested classes/interfaces inherited from class it.unimi.dsi.fastutil.objects.AbstractObjectList

    AbstractObjectList.ObjectRandomAccessSubList<K>, AbstractObjectList.ObjectSubList<K>
  • Constructor Summary

    Constructors
    Constructor Description
    ObjectImmutableList​(ObjectCollection<? extends K> c)
    Creates a new immutable list and fills it with a given type-specific collection.
    ObjectImmutableList​(ObjectIterator<? extends K> i)
    Creates a new immutable list and fills it with the elements returned by a type-specific iterator..
    ObjectImmutableList​(ObjectList<? extends K> l)
    Creates a new immutable list and fills it with a given type-specific list.
    ObjectImmutableList​(java.util.Collection<? extends K> c)
    Creates a new immutable list and fills it with a given collection.
    ObjectImmutableList​(K[] a)
    Creates a new immutable list using a given array.
    ObjectImmutableList​(K[] a, int offset, int length)
    Creates a new immutable list and fills it with the elements of a given array.
  • Method Summary

    Modifier and Type Method Description
    void add​(int index, K k)
    Deprecated. 
    boolean add​(K k)
    Deprecated. 
    boolean addAll​(int index, java.util.Collection<? extends K> c)
    Deprecated. 
    boolean addAll​(java.util.Collection<? extends K> c)
    Deprecated. 
    void addElements​(int index, K[] a, int offset, int length)
    Deprecated. 
    void clear()
    Deprecated. 
    ObjectImmutableList<K> clone()  
    int compareTo​(ObjectImmutableList<? extends K> l)
    Compares this immutable list to another immutable list.
    int compareTo​(java.util.List<? extends K> l)
    Compares this list to another object.
    boolean equals​(ObjectImmutableList<K> l)
    Compares this type-specific immutable list to another one.
    boolean equals​(java.lang.Object o)  
    void forEach​(java.util.function.Consumer<? super K> action)
    K get​(int index)  
    void getElements​(int from, java.lang.Object[] a, int offset, int length)
    Copies element of this type-specific list into the given array using optimized system calls.
    int indexOf​(java.lang.Object k)  
    boolean isEmpty()
    Checks whether the stack is empty.
    int lastIndexOf​(java.lang.Object k)  
    ObjectListIterator<K> listIterator​(int index)
    Returns a type-specific list iterator on the list starting at a given index.
    static <K> ObjectImmutableList<K> of()
    Returns an empty immutable list.
    static <K> ObjectImmutableList<K> of​(K... init)
    Creates an immutable list using a list of elements.
    K remove​(int index)
    Deprecated. 
    boolean remove​(java.lang.Object k)
    Deprecated. 
    boolean removeAll​(java.util.Collection<?> c)
    Deprecated. 
    void removeElements​(int from, int to)
    Deprecated. 
    boolean removeIf​(java.util.function.Predicate<? super K> c)
    Deprecated. 
    void replaceAll​(java.util.function.UnaryOperator<K> operator)
    Deprecated. 
    boolean retainAll​(java.util.Collection<?> c)
    Deprecated. 
    K set​(int index, K k)
    Deprecated. 
    void setElements​(int index, K[] a, int offset, int length)
    Deprecated. 
    int size()  
    void size​(int size)
    Deprecated. 
    void sort​(java.util.Comparator<? super K> comparator)
    Deprecated. 
    ObjectSpliterator<K> spliterator()
    Returns a type-specific spliterator on the elements of this list.
    ObjectList<K> subList​(int from, int to)
    Returns a type-specific view of the portion of this list from the index from, inclusive, to the index to, exclusive.
    java.lang.Object[] toArray()  
    <K> K[] toArray​(K[] a)  
    static <K> java.util.stream.Collector<K,​?,​ObjectImmutableList<K>> toList()
    Returns a Collector that collects a Stream's elements into a new ImmutableList.
    static <K> java.util.stream.Collector<K,​?,​ObjectImmutableList<K>> toListWithExpectedSize​(int expectedSize)
    Returns a Collector that collects a Stream's elements into a new ImmutableList, potentially pre-allocated to handle the given size.
    void unstableSort​(java.util.Comparator<? super K> comparator)
    Deprecated. 

    Methods inherited from class it.unimi.dsi.fastutil.objects.AbstractObjectList

    addElements, contains, hashCode, iterator, listIterator, peek, pop, push, top, toString

    Methods inherited from class java.util.AbstractCollection

    containsAll

    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.util.List

    add, add, addAll, addAll, clear, contains, containsAll, hashCode, remove, remove, removeAll, replaceAll, retainAll, set

    Methods inherited from interface it.unimi.dsi.fastutil.objects.ObjectList

    addAll, addAll, addElements, addElements, iterator, listIterator, removeElements, setElements, setElements, setElements, size, sort, unstableSort
  • Constructor Details

    • ObjectImmutableList

      public ObjectImmutableList​(K[] a)
      Creates a new immutable list using a given array.

      Note that this constructor does not perform a defensive copy.

      Parameters:
      a - the array that will be used to back this immutable list.
    • ObjectImmutableList

      public ObjectImmutableList​(java.util.Collection<? extends K> c)
      Creates a new immutable list and fills it with a given collection.
      Parameters:
      c - a collection that will be used to fill the immutable list.
    • ObjectImmutableList

      public ObjectImmutableList​(ObjectCollection<? extends K> c)
      Creates a new immutable list and fills it with a given type-specific collection.
      Parameters:
      c - a type-specific collection that will be used to fill the immutable list.
    • ObjectImmutableList

      public ObjectImmutableList​(ObjectList<? extends K> l)
      Creates a new immutable list and fills it with a given type-specific list.
      Parameters:
      l - a type-specific list that will be used to fill the immutable list.
    • ObjectImmutableList

      public ObjectImmutableList​(K[] a, int offset, int length)
      Creates a new immutable list and fills it with the elements of a given array.
      Parameters:
      a - an array whose elements will be used to fill the immutable list.
      offset - the first element to use.
      length - the number of elements to use.
    • ObjectImmutableList

      public ObjectImmutableList​(ObjectIterator<? extends K> i)
      Creates a new immutable list and fills it with the elements returned by a type-specific iterator..
      Parameters:
      i - a type-specific iterator whose returned elements will fill the immutable list.
  • Method Details

    • of

      public static <K> ObjectImmutableList<K> of()
      Returns an empty immutable list.
      Returns:
      an immutable list (possibly shared) that is empty.
    • of

      @SafeVarargs public static <K> ObjectImmutableList<K> of​(K... init)
      Creates an immutable list using a list of elements.

      Note that this method does not perform a defensive copy.

      Parameters:
      init - a list of elements that will be used to initialize the list.
      Returns:
      a new immutable list containing the given elements.
    • toList

      public static <K> java.util.stream.Collector<K,​?,​ObjectImmutableList<K>> toList()
      Returns a Collector that collects a Stream's elements into a new ImmutableList.
    • toListWithExpectedSize

      public static <K> java.util.stream.Collector<K,​?,​ObjectImmutableList<K>> toListWithExpectedSize​(int expectedSize)
      Returns a Collector that collects a Stream's elements into a new ImmutableList, potentially pre-allocated to handle the given size.
    • get

      public K get​(int index)
      Specified by:
      get in interface java.util.List<K>
    • indexOf

      public int indexOf​(java.lang.Object k)
      Specified by:
      indexOf in interface java.util.List<K>
      Overrides:
      indexOf in class AbstractObjectList<K>
    • lastIndexOf

      public int lastIndexOf​(java.lang.Object k)
      Specified by:
      lastIndexOf in interface java.util.List<K>
      Overrides:
      lastIndexOf in class AbstractObjectList<K>
    • size

      public int size()
      Specified by:
      size in interface java.util.Collection<K>
      Specified by:
      size in interface java.util.List<K>
      Specified by:
      size in class java.util.AbstractCollection<K>
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: Stack
      Checks whether the stack is empty.
      Specified by:
      isEmpty in interface java.util.Collection<K>
      Specified by:
      isEmpty in interface java.util.List<K>
      Specified by:
      isEmpty in interface Stack<K>
      Overrides:
      isEmpty in class java.util.AbstractCollection<K>
      Returns:
      true if the stack is empty.
    • getElements

      public void getElements​(int from, java.lang.Object[] a, int offset, int length)
      Copies element of this type-specific list into the given array using optimized system calls.
      Specified by:
      getElements in interface ObjectList<K>
      Overrides:
      getElements in class AbstractObjectList<K>
      Parameters:
      from - the start index (inclusive).
      a - the destination array.
      offset - the offset into the destination array where to store the first element copied.
      length - the number of elements to be copied.
    • forEach

      public void forEach​(java.util.function.Consumer<? super K> action)
      Description copied from class: AbstractObjectList
      Specified by:
      forEach in interface java.lang.Iterable<K>
      Overrides:
      forEach in class AbstractObjectList<K>
    • toArray

      public java.lang.Object[] toArray()
      Specified by:
      toArray in interface java.util.Collection<K>
      Specified by:
      toArray in interface java.util.List<K>
      Overrides:
      toArray in class AbstractObjectList<K>
    • toArray

      public <K> K[] toArray​(K[] a)
      Specified by:
      toArray in interface java.util.Collection<K>
      Specified by:
      toArray in interface java.util.List<K>
      Overrides:
      toArray in class AbstractObjectList<K>
    • listIterator

      public ObjectListIterator<K> listIterator​(int index)
      Description copied from class: AbstractObjectList
      Returns a type-specific list iterator on the list starting at a given index.
      Specified by:
      listIterator in interface java.util.List<K>
      Specified by:
      listIterator in interface ObjectList<K>
      Overrides:
      listIterator in class AbstractObjectList<K>
      See Also:
      List.listIterator(int)
    • spliterator

      public ObjectSpliterator<K> spliterator()
      Description copied from interface: ObjectList
      Returns a type-specific spliterator on the elements of this list.

      List spliterators must report at least Spliterator.SIZED and Spliterator.ORDERED.

      See List.spliterator() for more documentation on the requirements of the returned spliterator.

      Specified by:
      spliterator in interface java.util.Collection<K>
      Specified by:
      spliterator in interface java.lang.Iterable<K>
      Specified by:
      spliterator in interface java.util.List<K>
      Specified by:
      spliterator in interface ObjectCollection<K>
      Specified by:
      spliterator in interface ObjectIterable<K>
      Specified by:
      spliterator in interface ObjectList<K>
      Returns:
      a type-specific spliterator on the elements of this collection.
    • subList

      public ObjectList<K> subList​(int from, int to)
      Returns a type-specific view of the portion of this list from the index from, inclusive, to the index to, exclusive.
      Specified by:
      subList in interface java.util.List<K>
      Specified by:
      subList in interface ObjectList<K>
      Overrides:
      subList in class AbstractObjectList<K>
      See Also:
      List.subList(int,int)
      API Notes:
      The returned list will be immutable, but is currently not declared to return an instance of ImmutableList due to complications of implementation details. This may change in a future version (in other words, do not consider the return type of this method to be stable if making a subclass of ImmutableList).
    • clone

      public ObjectImmutableList<K> clone()
    • equals

      public boolean equals​(ObjectImmutableList<K> l)
      Compares this type-specific immutable list to another one.
      Parameters:
      l - a type-specific immutable list.
      Returns:
      true if the argument contains the same elements of this type-specific immutable list.
      API Notes:
      This method exists only for sake of efficiency. The implementation inherited from the abstract implementation would already work.
    • equals

      public boolean equals​(java.lang.Object o)
      Specified by:
      equals in interface java.util.Collection<K>
      Specified by:
      equals in interface java.util.List<K>
      Overrides:
      equals in class AbstractObjectList<K>
    • compareTo

      public int compareTo​(ObjectImmutableList<? extends K> l)
      Compares this immutable list to another immutable list.
      Parameters:
      l - an immutable list.
      Returns:
      a negative integer, zero, or a positive integer as this list is lexicographically less than, equal to, or greater than the argument.
      API Notes:
      This method exists only for sake of efficiency. The implementation inherited from the abstract implementation would already work.
    • compareTo

      public int compareTo​(java.util.List<? extends K> l)
      Description copied from class: AbstractObjectList
      Compares this list to another object. If the argument is a List, this method performs a lexicographical comparison; otherwise, it throws a ClassCastException.
      Specified by:
      compareTo in interface java.lang.Comparable<K>
      Overrides:
      compareTo in class AbstractObjectList<K>
      Parameters:
      l - a list.
      Returns:
      if the argument is a List, a negative integer, zero, or a positive integer as this list is lexicographically less than, equal to, or greater than the argument.
    • add

      @Deprecated public final void add​(int index, K k)
      Deprecated.
      Description copied from class: AbstractObjectList
      Specified by:
      add in interface java.util.List<K>
      Overrides:
      add in class AbstractObjectList<K>
      Implementation Specification:
      Always throws UnsupportedOperationException as this is an immutable type.
    • add

      @Deprecated public final boolean add​(K k)
      Deprecated.
      Description copied from class: AbstractObjectList
      Specified by:
      add in interface java.util.Collection<K>
      Specified by:
      add in interface java.util.List<K>
      Overrides:
      add in class AbstractObjectList<K>
      Implementation Specification:
      Always throws UnsupportedOperationException as this is an immutable type.
    • addAll

      @Deprecated public final boolean addAll​(java.util.Collection<? extends K> c)
      Deprecated.
      Description copied from class: AbstractObjectList
      Specified by:
      addAll in interface java.util.Collection<K>
      Specified by:
      addAll in interface java.util.List<K>
      Overrides:
      addAll in class AbstractObjectList<K>
      Implementation Specification:
      Always throws UnsupportedOperationException as this is an immutable type.
    • addAll

      @Deprecated public final boolean addAll​(int index, java.util.Collection<? extends K> c)
      Deprecated.
      Description copied from class: AbstractObjectList
      Adds all of the elements in the specified collection to this list (optional operation).
      Specified by:
      addAll in interface java.util.List<K>
      Overrides:
      addAll in class AbstractObjectList<K>
      Implementation Specification:
      Always throws UnsupportedOperationException as this is an immutable type.
    • remove

      @Deprecated public final K remove​(int index)
      Deprecated.
      Description copied from class: AbstractObjectList
      Specified by:
      remove in interface java.util.List<K>
      Overrides:
      remove in class AbstractObjectList<K>
      Implementation Specification:
      Always throws UnsupportedOperationException as this is an immutable type.
    • remove

      @Deprecated public final boolean remove​(java.lang.Object k)
      Deprecated.
      Specified by:
      remove in interface java.util.Collection<K>
      Specified by:
      remove in interface java.util.List<K>
      Overrides:
      remove in class java.util.AbstractCollection<K>
      Implementation Specification:
      Always throws UnsupportedOperationException as this is an immutable type.
    • removeAll

      @Deprecated public final boolean removeAll​(java.util.Collection<?> c)
      Deprecated.
      Specified by:
      removeAll in interface java.util.Collection<K>
      Specified by:
      removeAll in interface java.util.List<K>
      Overrides:
      removeAll in class java.util.AbstractCollection<K>
      Implementation Specification:
      Always throws UnsupportedOperationException as this is an immutable type.
    • retainAll

      @Deprecated public final boolean retainAll​(java.util.Collection<?> c)
      Deprecated.
      Specified by:
      retainAll in interface java.util.Collection<K>
      Specified by:
      retainAll in interface java.util.List<K>
      Overrides:
      retainAll in class java.util.AbstractCollection<K>
      Implementation Specification:
      Always throws UnsupportedOperationException as this is an immutable type.
    • removeIf

      @Deprecated public final boolean removeIf​(java.util.function.Predicate<? super K> c)
      Deprecated.
      Specified by:
      removeIf in interface java.util.Collection<K>
      Implementation Specification:
      Always throws UnsupportedOperationException as this is an immutable type.
    • replaceAll

      @Deprecated public final void replaceAll​(java.util.function.UnaryOperator<K> operator)
      Deprecated.
      Specified by:
      replaceAll in interface java.util.List<K>
      Implementation Specification:
      Always throws UnsupportedOperationException as this is an immutable type.
    • set

      @Deprecated public final K set​(int index, K k)
      Deprecated.
      Description copied from class: AbstractObjectList
      Specified by:
      set in interface java.util.List<K>
      Overrides:
      set in class AbstractObjectList<K>
      Implementation Specification:
      Always throws UnsupportedOperationException as this is an immutable type.
    • clear

      @Deprecated public final void clear()
      Deprecated.
      Description copied from class: AbstractObjectList
      Specified by:
      clear in interface java.util.Collection<K>
      Specified by:
      clear in interface java.util.List<K>
      Overrides:
      clear in class AbstractObjectList<K>
      Implementation Specification:
      Always throws UnsupportedOperationException as this is an immutable type.
    • size

      @Deprecated public final void size​(int size)
      Deprecated.
      Description copied from interface: ObjectList
      Sets the size of this list.

      If the specified size is smaller than the current size, the last elements are discarded. Otherwise, they are filled with 0/null/false.

      Specified by:
      size in interface ObjectList<K>
      Overrides:
      size in class AbstractObjectList<K>
      Parameters:
      size - the new size.
      Implementation Specification:
      Always throws UnsupportedOperationException as this is an immutable type.
    • removeElements

      @Deprecated public final void removeElements​(int from, int to)
      Deprecated.
      Description copied from class: AbstractObjectList
      Removes (hopefully quickly) elements of this type-specific list.

      This is a trivial iterator-based based implementation. It is expected that implementations will override this method with a more optimized version.

      Specified by:
      removeElements in interface ObjectList<K>
      Overrides:
      removeElements in class AbstractObjectList<K>
      Parameters:
      from - the start index (inclusive).
      to - the end index (exclusive).
      Implementation Specification:
      Always throws UnsupportedOperationException as this is an immutable type.
    • addElements

      @Deprecated public final void addElements​(int index, K[] a, int offset, int length)
      Deprecated.
      Description copied from class: AbstractObjectList
      Add (hopefully quickly) elements to this type-specific list.

      This is a trivial iterator-based implementation. It is expected that implementations will override this method with a more optimized version.

      Specified by:
      addElements in interface ObjectList<K>
      Overrides:
      addElements in class AbstractObjectList<K>
      Parameters:
      index - the index at which to add elements.
      a - the array containing the elements.
      offset - the offset of the first element to add.
      length - the number of elements to add.
      Implementation Specification:
      Always throws UnsupportedOperationException as this is an immutable type.
    • setElements

      @Deprecated public final void setElements​(int index, K[] a, int offset, int length)
      Deprecated.
      Description copied from interface: ObjectList
      Set (hopefully quickly) elements to match the array given. Sets each in this list to the corresponding elements in the array, as if by
       
       ListIterator iter = listIterator(index);
       int i = 0;
       while (i < length) {
         iter.next();
         iter.set(a[offset + i++]);
       }
       
       
      However, the exact implementation may be more efficient, taking into account whether random access is faster or not, or at the discretion of subclasses, abuse internals.
      Specified by:
      setElements in interface ObjectList<K>
      Overrides:
      setElements in class AbstractObjectList<K>
      Parameters:
      index - the index at which to start setting elements.
      a - the array containing the elements
      offset - the offset of the first element to add.
      length - the number of elements to add.
      Implementation Specification:
      Always throws UnsupportedOperationException as this is an immutable type.
    • sort

      @Deprecated public final void sort​(java.util.Comparator<? super K> comparator)
      Deprecated.
      Description copied from interface: ObjectList
      Sorts this list using a sort assured to be stable.

      Pass null to sort using natural ordering.

      Unless a subclass specifies otherwise, the results of the method if the list is concurrently modified during the sort are unspecified.

      Specified by:
      sort in interface java.util.List<K>
      Specified by:
      sort in interface ObjectList<K>
      Implementation Specification:
      Always throws UnsupportedOperationException as this is an immutable type.
    • unstableSort

      @Deprecated public final void unstableSort​(java.util.Comparator<? super K> comparator)
      Deprecated.
      Description copied from interface: ObjectList
      Sorts this list using a sort not assured to be stable. This differs from List.sort(java.util.Comparator) in that the results are not assured to be stable, but may be a bit faster.

      Pass null to sort using natural ordering.

      Unless a subclass specifies otherwise, the results of the method if the list is concurrently modified during the sort are unspecified.

      Specified by:
      unstableSort in interface ObjectList<K>
      Implementation Specification:
      Always throws UnsupportedOperationException as this is an immutable type.