Class ObjectArrayList<K>

All Implemented Interfaces:
ObjectCollection<K>, ObjectIterable<K>, ObjectList<K>, Stack<K>, Serializable, Cloneable, Comparable<List<? extends K>>, Iterable<K>, Collection<K>, List<K>, RandomAccess

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

This class implements a lightweight, fast, open, optimized, reuse-oriented version of array-based lists. Instances of this class represent a list with an array that is enlarged as needed when new entries are created (by increasing its current length by 50%), but is never made smaller (even on a clear()). A family of trimming methods lets you control the size of the backing array; this is particularly useful if you reuse instances of this class. Range checks are equivalent to those of java.util's classes, but they are delayed as much as possible.

The backing array is exposed by the elements() method. If an instance of this class was created by wrapping, backing-array reallocations will be performed using reflection, so that elements() can return an array of the same type of the original array: the comments about efficiency made in ObjectArrays apply here. Moreover, you must take into consideration that assignment to an array not of type Object[] is slower due to type checking.

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

See Also:
  • Field Details

    • DEFAULT_INITIAL_CAPACITY

      public static final int DEFAULT_INITIAL_CAPACITY
      The initial default capacity of an array list.
      See Also:
  • Constructor Details

    • ObjectArrayList

      public ObjectArrayList(int capacity)
      Creates a new array list with given capacity.
      Parameters:
      capacity - the initial capacity of the array list (may be 0).
    • ObjectArrayList

      public ObjectArrayList()
      Creates a new array list with DEFAULT_INITIAL_CAPACITY capacity.
    • ObjectArrayList

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

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

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

      public ObjectArrayList(K[] a)
      Creates a new array list and fills it with the elements of a given array.
      Parameters:
      a - an array whose elements will be used to fill the array list.
    • ObjectArrayList

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

      public ObjectArrayList(Iterator<? extends K> i)
      Creates a new array list and fills it with the elements returned by an iterator..
      Parameters:
      i - an iterator whose returned elements will fill the array list.
    • ObjectArrayList

      public ObjectArrayList(ObjectIterator<? extends K> i)
      Creates a new array 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 array list.
  • Method Details

    • elements

      public K[] elements()
      Returns the backing array of this list.

      If this array list was created by wrapping a given array, it is guaranteed that the type of the returned array will be the same. Otherwise, the returned array will be of type Object[] (in spite of the declared return type).

      Warning: This behaviour may cause (unfathomable) run-time errors if a method expects an array actually of type K[], but this methods returns an array of type Object[].

      Returns:
      the backing array.
    • wrap

      public static <K> ObjectArrayList<K> wrap(K[] a, int length)
      Wraps a given array into an array list of given size.

      Note it is guaranteed that the type of the array returned by elements() will be the same (see the comments in the class documentation).

      Parameters:
      a - an array to wrap.
      length - the length of the resulting array list.
      Returns:
      a new array list of the given size, wrapping the given array.
    • wrap

      public static <K> ObjectArrayList<K> wrap(K[] a)
      Wraps a given array into an array list.

      Note it is guaranteed that the type of the array returned by elements() will be the same (see the comments in the class documentation).

      Parameters:
      a - an array to wrap.
      Returns:
      a new array list wrapping the given array.
    • of

      public static <K> ObjectArrayList<K> of()
      Creates a new empty array list.
      Returns:
      a new empty array list.
    • of

      @SafeVarargs public static <K> ObjectArrayList<K> of(K... init)
      Creates an array list using an array of elements.
      Parameters:
      init - a the array the will become the new backing array of the array list.
      Returns:
      a new array list backed by the given array.
      See Also:
    • toList

      public static <K> Collector<K,?,ObjectArrayList<K>> toList()
      Returns a Collector that collects a Stream's elements into a new ArrayList.
    • toListWithExpectedSize

      public static <K> Collector<K,?,ObjectArrayList<K>> toListWithExpectedSize(int expectedSize)
      Returns a Collector that collects a Stream's elements into a new ArrayList, potentially pre-allocated to handle the given size.
    • ensureCapacity

      public void ensureCapacity(int capacity)
      Ensures that this array list can contain the given number of entries without resizing.
      Parameters:
      capacity - the new minimum capacity for this array list.
    • add

      public void add(int index, K k)
      Description copied from class: AbstractObjectList
      Specified by:
      add in interface List<K>
      Overrides:
      add in class AbstractObjectList<K>
    • add

      public boolean add(K k)
      Description copied from class: AbstractObjectList
      Specified by:
      add in interface Collection<K>
      Specified by:
      add in interface List<K>
      Overrides:
      add in class AbstractObjectList<K>
    • get

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

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

      public int lastIndexOf(Object k)
      Specified by:
      lastIndexOf in interface List<K>
      Overrides:
      lastIndexOf in class AbstractObjectList<K>
    • remove

      public K remove(int index)
      Description copied from class: AbstractObjectList
      Specified by:
      remove in interface List<K>
      Overrides:
      remove in class AbstractObjectList<K>
    • remove

      public boolean remove(Object k)
      Specified by:
      remove in interface Collection<K>
      Specified by:
      remove in interface List<K>
      Overrides:
      remove in class AbstractCollection<K>
    • set

      public K set(int index, K k)
      Description copied from class: AbstractObjectList
      Specified by:
      set in interface List<K>
      Overrides:
      set in class AbstractObjectList<K>
    • clear

      public void clear()
      Description copied from class: AbstractObjectList
      Specified by:
      clear in interface Collection<K>
      Specified by:
      clear in interface List<K>
      Overrides:
      clear in class AbstractObjectList<K>
    • size

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

      public void size(int size)
      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.
    • isEmpty

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

      public void trim()
      Trims this array list so that the capacity is equal to the size.
      See Also:
    • trim

      public void trim(int n)
      Trims the backing array if it is too large. If the current array length is smaller than or equal to n, this method does nothing. Otherwise, it trims the array length to the maximum between n and size().

      This method is useful when reusing lists. Clearing a list leaves the array length untouched. If you are reusing a list many times, you can call this method with a typical size to avoid keeping around a very large array just because of a few large transient lists.

      Parameters:
      n - the threshold for the trimming.
    • subList

      public ObjectList<K> subList(int from, int to)
      Description copied from interface: ObjectList
      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 List<K>
      Specified by:
      subList in interface ObjectList<K>
      Overrides:
      subList in class AbstractObjectList<K>
      See Also:
    • getElements

      public void getElements(int from, 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.
    • removeElements

      public void removeElements(int from, int to)
      Removes elements of this type-specific list using optimized system calls.
      Specified by:
      removeElements in interface ObjectList<K>
      Overrides:
      removeElements in class AbstractObjectList<K>
      Parameters:
      from - the start index (inclusive).
      to - the end index (exclusive).
    • addElements

      public void addElements(int index, K[] a, int offset, int length)
      Adds elements to this type-specific list using optimized system calls.
      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.
    • setElements

      public void setElements(int index, K[] a, int offset, int length)
      Sets elements to this type-specific list using optimized system calls.
      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.
    • forEach

      public void forEach(Consumer<? super K> action)
      Description copied from class: AbstractObjectList
      Specified by:
      forEach in interface Iterable<K>
      Overrides:
      forEach in class AbstractObjectList<K>
    • addAll

      public boolean addAll(int index, Collection<? extends K> c)
      Description copied from class: AbstractObjectList
      Adds all of the elements in the specified collection to this list (optional operation).
      Specified by:
      addAll in interface List<K>
      Overrides:
      addAll in class AbstractObjectList<K>
    • addAll

      public boolean addAll(int index, ObjectList<? extends K> l)
      Description copied from interface: ObjectList
      Inserts all of the elements in the specified type-specific list into this type-specific list at the specified position (optional operation).
      Specified by:
      addAll in interface ObjectList<K>
      See Also:
    • removeAll

      public boolean removeAll(Collection<?> c)
      Specified by:
      removeAll in interface Collection<K>
      Specified by:
      removeAll in interface List<K>
      Overrides:
      removeAll in class AbstractCollection<K>
    • toArray

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

      public <K> K[] toArray(K[] a)
      Specified by:
      toArray in interface Collection<K>
      Specified by:
      toArray in interface 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 List<K>
      Specified by:
      listIterator in interface ObjectList<K>
      Overrides:
      listIterator in class AbstractObjectList<K>
      See Also:
    • spliterator

      public ObjectSpliterator<K> spliterator()
      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.

      The returned spliterator is late-binding; it will track structural changes after the current index, up until the first trySplit(), at which point the maximum index will be fixed.
      Structural changes before the current index or after the first trySplit() will result in unspecified behavior.

      Specified by:
      spliterator in interface Collection<K>
      Specified by:
      spliterator in interface Iterable<K>
      Specified by:
      spliterator in interface 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.
    • sort

      public void sort(Comparator<? super K> comp)
      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 List<K>
      Specified by:
      sort in interface ObjectList<K>
    • unstableSort

      public void unstableSort(Comparator<? super K> comp)
      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>
    • clone

      public ObjectArrayList<K> clone()
    • equals

      public boolean equals(ObjectArrayList<K> l)
      Compares this type-specific array list to another one.
      Parameters:
      l - a type-specific array list.
      Returns:
      true if the argument contains the same elements of this type-specific array 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(Object o)
      Specified by:
      equals in interface Collection<K>
      Specified by:
      equals in interface List<K>
      Overrides:
      equals in class AbstractObjectList<K>
    • compareTo

      public int compareTo(ObjectArrayList<? extends K> l)
      Compares this array list to another array list.
      Parameters:
      l - an array 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(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 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.