public class ReferenceArrayList<K> extends AbstractReferenceList<K> implements java.util.RandomAccess, java.lang.Cloneable, java.io.Serializable
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.
ArrayList,
Serialized FormAbstractReferenceList.ReferenceRandomAccessSubList<K>, AbstractReferenceList.ReferenceSubList<K>| Modifier and Type | Field and Description |
|---|---|
protected K[] |
a
The backing array.
|
static int |
DEFAULT_INITIAL_CAPACITY
The initial default capacity of an array list.
|
protected int |
size
The current actual size of the list (never greater than the backing-array length).
|
protected boolean |
wrapped
Whether the backing array was passed to
wrap(). |
| Modifier | Constructor and Description |
|---|---|
|
ReferenceArrayList()
Creates a new array list with
DEFAULT_INITIAL_CAPACITY capacity. |
|
ReferenceArrayList(java.util.Collection<? extends K> c)
Creates a new array list and fills it with a given collection.
|
|
ReferenceArrayList(int capacity)
Creates a new array list with given capacity.
|
|
ReferenceArrayList(java.util.Iterator<? extends K> i)
Creates a new array list and fills it with the elements returned by an iterator..
|
|
ReferenceArrayList(K[] a)
Creates a new array list and fills it with the elements of a given array.
|
protected |
ReferenceArrayList(K[] a,
boolean wrapped)
Creates a new array list using a given array.
|
|
ReferenceArrayList(K[] a,
int offset,
int length)
Creates a new array list and fills it with the elements of a given array.
|
|
ReferenceArrayList(it.unimi.dsi.fastutil.objects.ObjectIterator<? extends K> i)
Creates a new array list and fills it with the elements returned by a type-specific iterator..
|
|
ReferenceArrayList(ReferenceCollection<? extends K> c)
Creates a new array list and fills it with a given type-specific collection.
|
|
ReferenceArrayList(ReferenceList<? extends K> l)
Creates a new array list and fills it with a given type-specific list.
|
| Modifier and Type | Method and Description |
|---|---|
void |
add(int index,
K k) |
boolean |
add(K k) |
boolean |
addAll(int index,
java.util.Collection<? extends K> c)
Adds all of the elements in the specified collection to this list (optional operation).
|
boolean |
addAll(int index,
ReferenceList<? extends K> l)
Inserts all of the elements in the specified type-specific list into this type-specific list at the specified position (optional operation).
|
void |
addElements(int index,
K[] a,
int offset,
int length)
Adds elements to this type-specific list using optimized system calls.
|
void |
clear() |
ReferenceArrayList<K> |
clone() |
K[] |
elements()
Returns the backing array of this list.
|
void |
ensureCapacity(int capacity)
Ensures that this array list can contain the given number of entries without resizing.
|
boolean |
equals(java.lang.Object o) |
boolean |
equals(ReferenceArrayList<K> l)
Compares this type-specific array list to another one.
|
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() |
int |
lastIndexOf(java.lang.Object k) |
it.unimi.dsi.fastutil.objects.ObjectListIterator<K> |
listIterator(int index)
Returns a type-specific list iterator on the list starting at a given index.
|
static <K> ReferenceArrayList<K> |
of()
Creates a new empty array list.
|
static <K> ReferenceArrayList<K> |
of(K... init)
Creates an array list using an array of elements.
|
K |
remove(int index) |
boolean |
remove(java.lang.Object k) |
boolean |
removeAll(java.util.Collection<?> c) |
void |
removeElements(int from,
int to)
Removes elements of this type-specific list using optimized system calls.
|
K |
set(int index,
K k) |
void |
setElements(int index,
K[] a,
int offset,
int length)
Sets elements to this type-specific list using optimized system calls.
|
int |
size() |
void |
size(int size)
Sets the size of this list.
|
void |
sort(java.util.Comparator<? super K> comp)
Sorts this list using a sort assured to be stable.
|
it.unimi.dsi.fastutil.objects.ObjectSpliterator<K> |
spliterator()
Returns a type-specific spliterator on the elements of this list.
|
ReferenceList<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() |
<T> T[] |
toArray(T[] a) |
static <K> java.util.stream.Collector<K,?,ReferenceArrayList<K>> |
toList()
Returns a
Collector that collects a Stream's elements into a new ArrayList. |
static <K> java.util.stream.Collector<K,?,ReferenceArrayList<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. |
void |
trim()
Trims this array list so that the capacity is equal to the size.
|
void |
trim(int n)
Trims the backing array if it is too large.
|
void |
unstableSort(java.util.Comparator<? super K> comp)
Sorts this list using a sort not assured to be stable.
|
static <K> ReferenceArrayList<K> |
wrap(K[] a)
Wraps a given array into an array list.
|
static <K> ReferenceArrayList<K> |
wrap(K[] a,
int length)
Wraps a given array into an array list of given size.
|
addAll, addElements, contains, ensureIndex, ensureRestrictedIndex, hashCode, iterator, listIterator, peek, pop, push, top, toStringfinalize, getClass, notify, notifyAll, wait, wait, waitaddAll, of, of, setElements, setElementspublic static final int DEFAULT_INITIAL_CAPACITY
protected final boolean wrapped
wrap(). In
this case, we must reallocate with the same type of array.protected transient K[] a
protected int size
protected ReferenceArrayList(K[] a, boolean wrapped)
This constructor is only meant to be used by the wrapping methods.
a - the array that will be used to back this array list.public ReferenceArrayList(int capacity)
capacity - the initial capacity of the array list (may be 0).public ReferenceArrayList()
DEFAULT_INITIAL_CAPACITY capacity.public ReferenceArrayList(java.util.Collection<? extends K> c)
c - a collection that will be used to fill the array list.public ReferenceArrayList(ReferenceCollection<? extends K> c)
c - a type-specific collection that will be used to fill the array list.public ReferenceArrayList(ReferenceList<? extends K> l)
l - a type-specific list that will be used to fill the array list.public ReferenceArrayList(K[] a)
a - an array whose elements will be used to fill the array list.public ReferenceArrayList(K[] a, int offset, int length)
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.public ReferenceArrayList(java.util.Iterator<? extends K> i)
i - an iterator whose returned elements will fill the array list.public ReferenceArrayList(it.unimi.dsi.fastutil.objects.ObjectIterator<? extends K> i)
i - a type-specific iterator whose returned elements will fill the array list.public K[] elements()
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[].
public static <K> ReferenceArrayList<K> wrap(K[] a, int length)
Note it is guaranteed
that the type of the array returned by elements() will be the same
(see the comments in the class documentation).
a - an array to wrap.length - the length of the resulting array list.public static <K> ReferenceArrayList<K> wrap(K[] a)
Note it is guaranteed
that the type of the array returned by elements() will be the same
(see the comments in the class documentation).
a - an array to wrap.public static <K> ReferenceArrayList<K> of()
of in interface ReferenceList<K>@SafeVarargs public static <K> ReferenceArrayList<K> of(K... init)
of in interface ReferenceList<K>init - a the array the will become the new backing array of the array list.wrap(K[], int)public static <K> java.util.stream.Collector<K,?,ReferenceArrayList<K>> toList()
Collector that collects a Stream's elements into a new ArrayList.public static <K> java.util.stream.Collector<K,?,ReferenceArrayList<K>> toListWithExpectedSize(int expectedSize)
Collector that collects a Stream's elements into a new ArrayList, potentially pre-allocated to handle the given size.public void ensureCapacity(int capacity)
capacity - the new minimum capacity for this array list.public void add(int index,
K k)
AbstractReferenceListadd in interface java.util.List<K>add in class AbstractReferenceList<K>public boolean add(K k)
AbstractReferenceListadd in interface java.util.Collection<K>add in interface java.util.List<K>add in class AbstractReferenceList<K>public int indexOf(java.lang.Object k)
indexOf in interface java.util.List<K>indexOf in class AbstractReferenceList<K>public int lastIndexOf(java.lang.Object k)
lastIndexOf in interface java.util.List<K>lastIndexOf in class AbstractReferenceList<K>public K remove(int index)
AbstractReferenceListremove in interface java.util.List<K>remove in class AbstractReferenceList<K>public boolean remove(java.lang.Object k)
public K set(int index, K k)
AbstractReferenceListset in interface java.util.List<K>set in class AbstractReferenceList<K>public void clear()
AbstractReferenceListclear in interface java.util.Collection<K>clear in interface java.util.List<K>clear in class AbstractReferenceList<K>public int size()
public void size(int size)
ReferenceListIf the specified size is smaller than the current size, the last elements are
discarded. Otherwise, they are filled with 0/null/false.
size in interface ReferenceList<K>size in class AbstractReferenceList<K>size - the new size.public boolean isEmpty()
public void trim()
ArrayList.trimToSize()public void trim(int n)
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.
n - the threshold for the trimming.public ReferenceList<K> subList(int from, int to)
ReferenceListfrom, inclusive, to the index to, exclusive.subList in interface ReferenceList<K>subList in interface java.util.List<K>subList in class AbstractReferenceList<K>List.subList(int,int)public void getElements(int from,
java.lang.Object[] a,
int offset,
int length)
getElements in interface ReferenceList<K>getElements in class AbstractReferenceList<K>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.public void removeElements(int from,
int to)
removeElements in interface ReferenceList<K>removeElements in class AbstractReferenceList<K>from - the start index (inclusive).to - the end index (exclusive).public void addElements(int index,
K[] a,
int offset,
int length)
addElements in interface ReferenceList<K>addElements in class AbstractReferenceList<K>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.public void setElements(int index,
K[] a,
int offset,
int length)
setElements in interface ReferenceList<K>setElements in class AbstractReferenceList<K>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.public void forEach(java.util.function.Consumer<? super K> action)
AbstractReferenceListforEach in interface java.lang.Iterable<K>forEach in class AbstractReferenceList<K>public boolean addAll(int index,
java.util.Collection<? extends K> c)
AbstractReferenceListaddAll in interface java.util.List<K>addAll in class AbstractReferenceList<K>public boolean addAll(int index,
ReferenceList<? extends K> l)
ReferenceListaddAll in interface ReferenceList<K>List.addAll(int,Collection)public boolean removeAll(java.util.Collection<?> c)
public java.lang.Object[] toArray()
toArray in interface java.util.Collection<K>toArray in interface java.util.List<K>toArray in class AbstractReferenceList<K>public <T> T[] toArray(T[] a)
toArray in interface java.util.Collection<K>toArray in interface java.util.List<K>toArray in class AbstractReferenceList<K>public it.unimi.dsi.fastutil.objects.ObjectListIterator<K> listIterator(int index)
AbstractReferenceListlistIterator in interface ReferenceList<K>listIterator in interface java.util.List<K>listIterator in class AbstractReferenceList<K>List.listIterator(int)public it.unimi.dsi.fastutil.objects.ObjectSpliterator<K> spliterator()
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.
spliterator in interface it.unimi.dsi.fastutil.objects.ObjectIterable<K>spliterator in interface ReferenceCollection<K>spliterator in interface ReferenceList<K>spliterator in interface java.lang.Iterable<K>spliterator in interface java.util.Collection<K>spliterator in interface java.util.List<K>public void sort(java.util.Comparator<? super K> comp)
ReferenceListPass 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.
sort in interface ReferenceList<K>sort in interface java.util.List<K>public void unstableSort(java.util.Comparator<? super K> comp)
ReferenceListList.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.
unstableSort in interface ReferenceList<K>public ReferenceArrayList<K> clone()
clone in class java.lang.Objectpublic boolean equals(ReferenceArrayList<K> l)
l - a type-specific array list.public boolean equals(java.lang.Object o)
equals in interface java.util.Collection<K>equals in interface java.util.List<K>equals in class AbstractReferenceList<K>