public class JoddArrayList<E>
extends java.util.AbstractList<E>
implements java.util.RandomAccess, java.lang.Cloneable
ArrayList
and LinkedList
.
ArrayList
performs slow when elements are added in the middle of the list.
Adding element to the first position if slow as the whole buffer has to be moved.
LinkedList
is slow for random access and always takes more memory then
ArrayList
.
This implementation gives better performances as it can grow both sides: left and right. Its not circular, like some other list implementations out there, so the implementation can remain rather simple. Moreover, if that make more sense, list will not grow, but data will be moved internally to fill up the gaps.
This implementation can be fine-tuned. There are the following parameters:
pivotType
- pivot splits the list on the left and right side. There
are 3 types of pivots, depending how do you expect the list will be populated
minimalGrowSize
- font grow for less then this number.maxFreeSpaceBeforeNormalize
- if list has at least this much of space,
don't grow it, but move the buffer data.
Modifier and Type | Class and Description |
---|---|
static class |
JoddArrayList.PIVOT_TYPE
Defines pivot point types.
|
Modifier and Type | Field and Description |
---|---|
protected java.lang.Object[] |
buffer |
protected int |
end |
protected int |
maxFreeSpaceBeforeNormalize |
protected int |
minimalGrowSize |
protected int |
pivotIndex |
protected JoddArrayList.PIVOT_TYPE |
pivotType |
protected int |
size |
protected int |
start |
Constructor and Description |
---|
JoddArrayList()
Constructs an empty list with an initial capacity of ten.
|
JoddArrayList(java.util.Collection<? extends E> collection)
Constructs a list containing the elements of the specified
collection, in the order they are returned by the collection's
iterator.
|
JoddArrayList(E... array)
Constructs a list containing the elements of provided array.
|
JoddArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.
|
JoddArrayList(int initialCapacity,
JoddArrayList.PIVOT_TYPE pivot_type,
int minimalGrowSize,
int maxFreeSpaceBeforeNormalize)
Constructs fine-tuned list.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E e)
Appends the specified element to the end of this list.
|
void |
add(int index,
E element)
Inserts the specified element at the specified position in this
list.
|
boolean |
addAll(java.util.Collection<? extends E> collection)
Appends all of the elements in the specified collection to the end of
this list, in the order that they are returned by the
specified collection's Iterator.
|
boolean |
addAll(E... array)
Appends all elements of given array.
|
boolean |
addAll(int index,
java.util.Collection<? extends E> collection)
Inserts all of the elements in the specified collection into this
list, starting at the specified position.
|
boolean |
addAll(int index,
E... array)
Inserts all array elements to this list.
|
boolean |
addFirst(E e)
Adds the specified element to the beginning of this list.
|
boolean |
addLast(E e)
Appends element to the list.
|
protected boolean |
batchRemove(java.util.Collection<?> collection,
boolean complement) |
void |
clear()
Removes all of the elements from this list.
|
JoddArrayList<E> |
clone()
Returns a shallow copy of this
ArrayList instance. |
boolean |
contains(java.lang.Object o)
Returns
true if this list contains the specified element. |
protected boolean |
doAddAll(int index,
java.lang.Object[] array) |
protected boolean |
doAddAll(java.lang.Object[] array) |
protected E |
doRemove(int index) |
protected void |
ensureCapacity(int index,
int elementsToAdd)
Ensures that buffer size will handle addition of
elementsToAdd elements
on provided index position. |
E |
get(int index)
Returns the element at the specified position in this list.
|
E |
getFirst()
Returns the first element in the list.
|
E |
getLast()
Returns the last element of the list.
|
int |
indexOf(java.lang.Object o)
Returns the index of the first occurrence of the specified element
in this list, or -1 if this list does not contain the element.
|
protected void |
init(int capacity)
Initializes the list.
|
boolean |
isEmpty()
Returns
true if this list contains no elements. |
java.util.Iterator<E> |
iterator()
Returns an iterator over the elements in this list in proper sequence.
|
int |
lastIndexOf(java.lang.Object o)
Returns the index of the last occurrence of the specified element
in this list, or -1 if this list does not contain the element.
|
java.util.ListIterator<E> |
listIterator()
Returns a list iterator over the elements in this list (in proper
sequence).
|
java.util.ListIterator<E> |
listIterator(int index)
Returns a list iterator over the elements in this list (in proper
sequence), starting at the specified position in the list.
|
protected void |
normalize()
Normalizes list by moving the content into ideal position
balanced over pivot.
|
E |
remove(int index)
Removes the element at the specified position in this list.
|
boolean |
remove(java.lang.Object o)
Removes the first occurrence of the specified element from this list,
if it is present.
|
boolean |
removeAll(java.util.Collection<?> c)
Removes from this list all of its elements that are contained in the
specified collection.
|
E |
removeFirst()
Removes first element of the list.
|
E |
removeLast()
Removes last element of the list.
|
protected void |
removeRange(int fromIndex,
int toIndex)
Removes from this list all of the elements whose index is between
fromIndex , inclusive, and toIndex , exclusive. |
boolean |
retainAll(java.util.Collection<?> c)
Retains only the elements in this list that are contained in the
specified collection.
|
E |
set(int index,
E element)
Replaces the element at the specified position in this list with
the specified element.
|
int |
size()
Returns the number of elements in this list.
|
java.lang.Object[] |
toArray()
Returns an array containing all of the elements in this list
in proper sequence (from first to last element).
|
<T> T[] |
toArray(T[] array)
Returns an array containing all of the elements in this list in proper
sequence (from first to last element); the runtime type of the returned
array is that of the specified array.
|
java.lang.String |
toString()
Returns string representation of this array.
|
void |
trimToSize()
Trims the capacity of this
ArrayList instance to be the
list's current size. |
protected java.lang.Object[] buffer
protected int size
protected int start
protected int end
protected int pivotIndex
protected JoddArrayList.PIVOT_TYPE pivotType
protected int minimalGrowSize
protected int maxFreeSpaceBeforeNormalize
public JoddArrayList(int initialCapacity)
public JoddArrayList(int initialCapacity, JoddArrayList.PIVOT_TYPE pivot_type, int minimalGrowSize, int maxFreeSpaceBeforeNormalize)
public JoddArrayList()
public JoddArrayList(java.util.Collection<? extends E> collection)
public JoddArrayList(E... array)
protected void init(int capacity)
public void trimToSize()
ArrayList
instance to be the
list's current size. An application can use this operation to minimize
the storage of an ArrayList
instance.protected void normalize()
protected void ensureCapacity(int index, int elementsToAdd)
elementsToAdd
elements
on provided index
position.public int size()
public boolean isEmpty()
true
if this list contains no elements.public boolean contains(java.lang.Object o)
true
if this list contains the specified element.public int indexOf(java.lang.Object o)
i
such that
(o==null ? get(i)==null : o.equals(get(i)))
,
or -1 if there is no such index.public int lastIndexOf(java.lang.Object o)
public JoddArrayList<E> clone()
ArrayList
instance. (The
elements themselves are not copied.)clone
in class java.lang.Object
public java.lang.Object[] toArray()
public <T> T[] toArray(T[] array)
public E get(int index)
public E getFirst()
public E getLast()
public E set(int index, E element)
public boolean add(E e)
public boolean addFirst(E e)
public boolean addLast(E e)
public void add(int index, E element)
public boolean addAll(java.util.Collection<? extends E> collection)
public boolean addAll(E... array)
protected boolean doAddAll(java.lang.Object[] array)
public boolean addAll(int index, java.util.Collection<? extends E> collection)
public boolean addAll(int index, E... array)
protected boolean doAddAll(int index, java.lang.Object[] array)
public void clear()
public E removeFirst()
public E removeLast()
public E remove(int index)
protected E doRemove(int index)
public boolean remove(java.lang.Object o)
protected void removeRange(int fromIndex, int toIndex)
fromIndex
, inclusive, and toIndex
, exclusive.
Shifts any succeeding elements to the left (reduces their index).removeRange
in class java.util.AbstractList<E>
public boolean removeAll(java.util.Collection<?> c)
public boolean retainAll(java.util.Collection<?> c)
protected boolean batchRemove(java.util.Collection<?> collection, boolean complement)
public java.util.ListIterator<E> listIterator(int index)
public java.util.ListIterator<E> listIterator()
listIterator
in interface java.util.List<E>
listIterator
in class java.util.AbstractList<E>
listIterator(int)
public java.util.Iterator<E> iterator()
public java.lang.String toString()
toString
in class java.util.AbstractCollection<E>
Copyright © 2003-present Jodd Team