Class LongArrayList

  • All Implemented Interfaces:
    java.lang.Iterable<java.lang.Long>, java.util.Collection<java.lang.Long>, java.util.List<java.lang.Long>, java.util.RandomAccess

    public class LongArrayList
    extends java.util.AbstractList<java.lang.Long>
    implements java.util.List<java.lang.Long>, java.util.RandomAccess
    A List implementation that stores long values with the ability to not have them boxed.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static long DEFAULT_NULL_VALUE
      The default value that will be used in place of null for an element.
      private long[] elements  
      static int INITIAL_CAPACITY
      Initial capacity to which the array will be sized.
      private long nullValue  
      private int size  
      • Fields inherited from class java.util.AbstractList

        modCount
    • Constructor Summary

      Constructors 
      Constructor Description
      LongArrayList()
      Constructs a new list with the INITIAL_CAPACITY using DEFAULT_NULL_VALUE.
      LongArrayList​(int initialCapacity, long nullValue)
      Construct a new list.
      LongArrayList​(long[] initialElements, int initialSize, long nullValue)
      Create a new list that wraps an existing arrays without copying it.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int index, java.lang.Long element)
      boolean add​(java.lang.Long element)
      boolean addAll​(int index, LongArrayList list)
      Inserts all the elements from the specified list to this list at the specified position.
      boolean addAll​(LongArrayList list)
      Appends all the elements in the specified list to the end of this list, in the order that they are stored in the specified list.
      void addLong​(int index, long element)
      Add an element without boxing at a given index.
      boolean addLong​(long element)
      Add an element without boxing.
      int capacity()
      The current capacity for the collection.
      private void checkIndex​(int index)  
      private void checkIndexForAdd​(int index)  
      void clear()
      boolean contains​(java.lang.Object o)
      boolean containsAll​(LongArrayList list)
      Returns true if this list contains all the elements of the specified list.
      boolean containsLong​(long value)
      Does the list contain this element value.
      void ensureCapacity​(int requiredCapacity)
      Ensure the backing array has a required capacity.
      private void ensureCapacityPrivate​(int requiredCapacity)  
      boolean equals​(java.lang.Object other)
      boolean equals​(LongArrayList that)
      Type-safe overload of the equals(Object) method.
      long fastUnorderedRemove​(int index)
      Removes element at index, but instead of copying all elements to the left, it replaces the item in the slot with the last item in the list.
      boolean fastUnorderedRemoveLong​(long value)
      Remove the first instance of a value if found in the list and replaces it with the last item in the list.
      void forEach​(java.util.function.Consumer<? super java.lang.Long> action)
      void forEachLong​(java.util.function.LongConsumer action)
      Iterate over the collection without boxing.
      void forEachOrderedLong​(java.util.function.LongConsumer action)
      For each element in order provide the long value to a LongConsumer.
      java.lang.Long get​(int index)
      long getLong​(int index)
      Get the element at a given index without boxing.
      int hashCode()
      int indexOf​(long value)
      Index of the first element with this value.
      int lastIndexOf​(long value)
      Index of the last element with this value.
      java.util.stream.LongStream longStream()
      Create a LongStream over the elements of underlying array.
      long nullValue()
      The value representing a null element.
      long popLong()
      Pop a value off the end of the array as a stack operation.
      void pushLong​(long element)
      Push an element onto the end of the array like a stack.
      java.lang.Long remove​(int index)
      Remove at a given index.
      boolean remove​(java.lang.Object o)
      boolean removeAll​(LongArrayList list)
      Removes all of this collection's elements that are also contained in the specified list.
      long removeAt​(int index)
      Remove at a given index.
      boolean removeIfLong​(java.util.function.LongPredicate filter)
      Removes all the elements of this collection that satisfy the given predicate.
      boolean removeLong​(long value)
      Remove the first instance of a value if found in the list.
      boolean retainAll​(LongArrayList list)
      Retains only the elements in this list that are contained in the specified list.
      java.lang.Long set​(int index, java.lang.Long element)
      long setLong​(int index, long element)
      Set an element at a given index without boxing.
      int size()
      long[] toLongArray()
      Create a new array that is a copy of the elements.
      long[] toLongArray​(long[] dst)
      Create a new array that is a copy of the elements.
      java.lang.String toString()
      void trimToSize()
      Trim the underlying array to be the current size, or INITIAL_CAPACITY if size is less.
      void wrap​(long[] initialElements, int initialSize)
      Wrap an existing array without copying it.
      • Methods inherited from class java.util.AbstractList

        addAll, indexOf, iterator, lastIndexOf, listIterator, listIterator, removeRange, subList
      • Methods inherited from class java.util.AbstractCollection

        addAll, containsAll, isEmpty, removeAll, retainAll, toArray, toArray
      • 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.util.List

        addAll, addAll, containsAll, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, removeAll, replaceAll, retainAll, sort, spliterator, subList, toArray, toArray
    • Field Detail

      • DEFAULT_NULL_VALUE

        public static final long DEFAULT_NULL_VALUE
        The default value that will be used in place of null for an element.
        See Also:
        Constant Field Values
      • INITIAL_CAPACITY

        public static final int INITIAL_CAPACITY
        Initial capacity to which the array will be sized.
        See Also:
        Constant Field Values
      • nullValue

        private final long nullValue
      • size

        private int size
      • elements

        private long[] elements
    • Constructor Detail

      • LongArrayList

        public LongArrayList​(int initialCapacity,
                             long nullValue)
        Construct a new list.
        Parameters:
        initialCapacity - for the backing array.
        nullValue - to be used to represent a null element.
      • LongArrayList

        public LongArrayList​(long[] initialElements,
                             int initialSize,
                             long nullValue)
        Create a new list that wraps an existing arrays without copying it.
        Parameters:
        initialElements - to be wrapped.
        initialSize - of the array to wrap.
        nullValue - to be used to represent a null element.
    • Method Detail

      • wrap

        public void wrap​(long[] initialElements,
                         int initialSize)
        Wrap an existing array without copying it.
        Parameters:
        initialElements - to be wrapped.
        initialSize - of the array to wrap.
        Throws:
        java.lang.IllegalArgumentException - if the initialSize is less than 0 or greater than the length of the initial array.
      • nullValue

        public long nullValue()
        The value representing a null element.
        Returns:
        value representing a null element.
      • size

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

        public int capacity()
        The current capacity for the collection.
        Returns:
        the current capacity for the collection.
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<java.lang.Long>
        Specified by:
        clear in interface java.util.List<java.lang.Long>
        Overrides:
        clear in class java.util.AbstractList<java.lang.Long>
      • trimToSize

        public void trimToSize()
        Trim the underlying array to be the current size, or INITIAL_CAPACITY if size is less.
      • get

        public java.lang.Long get​(int index)
        Specified by:
        get in interface java.util.List<java.lang.Long>
        Specified by:
        get in class java.util.AbstractList<java.lang.Long>
      • getLong

        public long getLong​(int index)
        Get the element at a given index without boxing.
        Parameters:
        index - to get.
        Returns:
        the unboxed element.
      • add

        public boolean add​(java.lang.Long element)
        Specified by:
        add in interface java.util.Collection<java.lang.Long>
        Specified by:
        add in interface java.util.List<java.lang.Long>
        Overrides:
        add in class java.util.AbstractList<java.lang.Long>
      • addLong

        public boolean addLong​(long element)
        Add an element without boxing.
        Parameters:
        element - to be added.
        Returns:
        true
      • add

        public void add​(int index,
                        java.lang.Long element)
        Specified by:
        add in interface java.util.List<java.lang.Long>
        Overrides:
        add in class java.util.AbstractList<java.lang.Long>
      • addLong

        public void addLong​(int index,
                            long element)
        Add an element without boxing at a given index.
        Parameters:
        index - at which the element should be added.
        element - to be added.
      • set

        public java.lang.Long set​(int index,
                                  java.lang.Long element)
        Specified by:
        set in interface java.util.List<java.lang.Long>
        Overrides:
        set in class java.util.AbstractList<java.lang.Long>
      • setLong

        public long setLong​(int index,
                            long element)
        Set an element at a given index without boxing.
        Parameters:
        index - at which to set the element.
        element - to be added.
        Returns:
        the previous element at the index.
      • contains

        public boolean contains​(java.lang.Object o)
        Specified by:
        contains in interface java.util.Collection<java.lang.Long>
        Specified by:
        contains in interface java.util.List<java.lang.Long>
        Overrides:
        contains in class java.util.AbstractCollection<java.lang.Long>
      • containsLong

        public boolean containsLong​(long value)
        Does the list contain this element value.
        Parameters:
        value - of the element.
        Returns:
        true if present otherwise false.
      • indexOf

        public int indexOf​(long value)
        Index of the first element with this value.
        Parameters:
        value - for the element.
        Returns:
        the index if found otherwise -1.
      • lastIndexOf

        public int lastIndexOf​(long value)
        Index of the last element with this value.
        Parameters:
        value - for the element.
        Returns:
        the index if found otherwise -1.
      • addAll

        public boolean addAll​(LongArrayList list)
        Appends all the elements in the specified list to the end of this list, in the order that they are stored in the specified list.
        Parameters:
        list - containing elements to be added to this list.
        Returns:
        true if this list changed as a result of the call.
      • addAll

        public boolean addAll​(int index,
                              LongArrayList list)
        Inserts all the elements from the specified list to this list at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in this list in the order that they are stored in the specified list.
        Parameters:
        index - at which to insert the first element from the specified collection.
        list - containing elements to be added to this list.
        Returns:
        true if this list changed as a result of the call.
      • containsAll

        public boolean containsAll​(LongArrayList list)
        Returns true if this list contains all the elements of the specified list.
        Parameters:
        list - to be checked for containment in this list.
        Returns:
        true if this list contains all the elements of the specified list.
      • retainAll

        public boolean retainAll​(LongArrayList list)
        Retains only the elements in this list that are contained in the specified list. In other words, removes from this list all of its elements that are not contained in the specified list.
        Parameters:
        list - containing elements to be removed from this list.
        Returns:
        true if this list changed as a result of the call.
      • removeAll

        public boolean removeAll​(LongArrayList list)
        Removes all of this collection's elements that are also contained in the specified list. After this call returns, this list will contain no elements in common with the specified list.
        Parameters:
        list - whose elements are to be removed from this list.
        Returns:
        true if this list changed as a result of the call.
      • removeIfLong

        public boolean removeIfLong​(java.util.function.LongPredicate filter)
        Removes all the elements of this collection that satisfy the given predicate.
        Parameters:
        filter - a predicate which returns true for elements to be removed.
        Returns:
        true if any elements were removed.
      • remove

        public boolean remove​(java.lang.Object o)
        Specified by:
        remove in interface java.util.Collection<java.lang.Long>
        Specified by:
        remove in interface java.util.List<java.lang.Long>
        Overrides:
        remove in class java.util.AbstractCollection<java.lang.Long>
      • remove

        public java.lang.Long remove​(int index)
        Remove at a given index.
        Specified by:
        remove in interface java.util.List<java.lang.Long>
        Overrides:
        remove in class java.util.AbstractList<java.lang.Long>
        Parameters:
        index - of the element to be removed.
        Returns:
        the existing value at this index.
      • removeAt

        public long removeAt​(int index)
        Remove at a given index.
        Parameters:
        index - of the element to be removed.
        Returns:
        the existing value at this index.
      • fastUnorderedRemove

        public long fastUnorderedRemove​(int index)
        Removes element at index, but instead of copying all elements to the left, it replaces the item in the slot with the last item in the list. This avoids the copy costs at the expense of preserving list order. If index is the last element it is just removed.
        Parameters:
        index - of the element to be removed.
        Returns:
        the existing value at this index.
        Throws:
        java.lang.IndexOutOfBoundsException - if index is out of bounds.
      • removeLong

        public boolean removeLong​(long value)
        Remove the first instance of a value if found in the list.

        Primitive specialization of the List.remove(Object) method.

        Parameters:
        value - to be removed.
        Returns:
        true if successful otherwise false.
      • fastUnorderedRemoveLong

        public boolean fastUnorderedRemoveLong​(long value)
        Remove the first instance of a value if found in the list and replaces it with the last item in the list. This saves a copy down of all items at the expense of not preserving list order.
        Parameters:
        value - to be removed.
        Returns:
        true if successful otherwise false.
      • pushLong

        public void pushLong​(long element)
        Push an element onto the end of the array like a stack.
        Parameters:
        element - to be pushed onto the end of the array.
      • popLong

        public long popLong()
        Pop a value off the end of the array as a stack operation.
        Returns:
        the value at the end of the array.
        Throws:
        java.util.NoSuchElementException - if the array is empty.
      • forEachOrderedLong

        public void forEachOrderedLong​(java.util.function.LongConsumer action)
        For each element in order provide the long value to a LongConsumer.
        Parameters:
        action - to be taken for each element.
      • longStream

        public java.util.stream.LongStream longStream()
        Create a LongStream over the elements of underlying array.
        Returns:
        a LongStream over the elements of underlying array.
      • toLongArray

        public long[] toLongArray()
        Create a new array that is a copy of the elements.
        Returns:
        a copy of the elements.
      • toLongArray

        public long[] toLongArray​(long[] dst)
        Create a new array that is a copy of the elements.
        Parameters:
        dst - destination array for the copy if it is the correct size.
        Returns:
        a copy of the elements.
      • ensureCapacity

        public void ensureCapacity​(int requiredCapacity)
        Ensure the backing array has a required capacity.
        Parameters:
        requiredCapacity - for the backing array.
      • equals

        public boolean equals​(LongArrayList that)
        Type-safe overload of the equals(Object) method.
        Parameters:
        that - other list.
        Returns:
        true if lists are equal.
      • equals

        public boolean equals​(java.lang.Object other)
        Specified by:
        equals in interface java.util.Collection<java.lang.Long>
        Specified by:
        equals in interface java.util.List<java.lang.Long>
        Overrides:
        equals in class java.util.AbstractList<java.lang.Long>
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Collection<java.lang.Long>
        Specified by:
        hashCode in interface java.util.List<java.lang.Long>
        Overrides:
        hashCode in class java.util.AbstractList<java.lang.Long>
      • forEach

        public void forEach​(java.util.function.Consumer<? super java.lang.Long> action)
        Specified by:
        forEach in interface java.lang.Iterable<java.lang.Long>
      • forEachLong

        public void forEachLong​(java.util.function.LongConsumer action)
        Iterate over the collection without boxing.
        Parameters:
        action - to be taken for each element.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.util.AbstractCollection<java.lang.Long>
      • ensureCapacityPrivate

        private void ensureCapacityPrivate​(int requiredCapacity)
      • checkIndex

        private void checkIndex​(int index)
      • checkIndexForAdd

        private void checkIndexForAdd​(int index)