public class KTypeArrayList<KType> extends java.lang.Object implements KTypeIndexedContainer<KType>, java.lang.Cloneable
ArraySizingStrategy
and may be expensive if they move around really large chunks of memory.
#if ($TemplateOptions.KTypeGeneric)
A brief comparison of the API against the Java Collections framework:
java.util.ArrayList | ObjectArrayList |
---|---|
add | add |
add(index,v) | insert(index,v) |
get | get |
removeRange, removeElementAt | removeRange, remove |
remove(Object) | removeFirstOccurrence, removeLastOccurrence, removeAllOccurrences |
clear | clear, release |
size | size |
ensureCapacity | ensureCapacity, resize |
indexOf | indexOf |
lastIndexOf | lastIndexOf |
trimToSize | trimtoSize |
Object[] toArray() | KType[] toArray() |
iterator | cursor over values |
See ObjectArrayList
class for API similarities and differences against Java
Collections.
#end
Modifier and Type | Field and Description |
---|---|
KType[] |
buffer
Internal array for storing the list.
|
static int |
DEFAULT_CAPACITY
Default capacity if no other capacity is given in the constructor.
|
int |
elementsCount
Current number of elements stored in
buffer . |
protected ArraySizingStrategy |
resizer
Buffer resizing strategy.
|
Constructor and Description |
---|
KTypeArrayList()
Create with default sizing strategy and initial capacity for storing
5 elements.
|
KTypeArrayList(int initialCapacity)
Create with default sizing strategy and the given initial capacity.
|
KTypeArrayList(int initialCapacity,
ArraySizingStrategy resizer)
Create with a custom buffer resizing strategy.
|
KTypeArrayList(KTypeContainer<? extends KType> container)
Creates a new list from elements of another container.
|
Modifier and Type | Method and Description |
---|---|
void |
add(KType... elements)
Vararg-signature method for adding elements at the end of the list.
|
void |
add(KType e1)
Adds an element to the end of this container (the last index is incremented by one).
|
void |
add(KType[] elements,
int start,
int length)
Add all elements from a range of given array to the list.
|
void |
add(KType e1,
KType e2)
Appends two elements at the end of the list.
|
int |
addAll(java.lang.Iterable<? extends KTypeCursor<? extends KType>> iterable)
Adds all elements from another iterable.
|
int |
addAll(KTypeContainer<? extends KType> container)
Adds all elements from another container.
|
void |
clear()
Sets the number of stored elements to zero.
|
KTypeArrayList<KType> |
clone()
Clone this object.
|
boolean |
contains(KType e1)
Lookup a given element in the container.
|
protected void |
ensureBufferSpace(int expectedAdditions)
Ensures the internal buffer has enough free slots to store
expectedAdditions . |
void |
ensureCapacity(int minCapacity)
Increases the capacity of this instance, if necessary, to ensure
that it can hold at least the number of elements specified by
the minimum capacity argument.
|
boolean |
equals(java.lang.Object obj)
Compares the specified object with this container for equality.
|
<T extends KTypePredicate<? super KType>> |
forEach(T predicate)
Applies a
predicate to container elements as long, as the predicate
returns true . |
<T extends KTypeProcedure<? super KType>> |
forEach(T procedure)
Applies a
procedure to all container elements. |
<T extends KTypePredicate<? super KType>> |
forEach(T predicate,
int fromIndex,
int toIndex)
Applies
predicate to a slice of the list,
fromIndex , inclusive, to toIndex ,
exclusive, or until predicate returns false . |
<T extends KTypeProcedure<? super KType>> |
forEach(T procedure,
int fromIndex,
int toIndex)
Applies
procedure to a slice of the list,
fromIndex , inclusive, to toIndex ,
exclusive. |
static <KType> KTypeArrayList<KType> |
from(KType... elements)
Create a list from a variable number of arguments or an array of
KType . |
static <KType> KTypeArrayList<KType> |
from(KTypeContainer<KType> container)
Create a list from elements of another container.
|
KType |
get(int index) |
int |
hashCode() |
int |
indexOf(KType e1)
Returns the index of the first occurrence of the specified element in this list,
or -1 if this list does not contain the element.
|
void |
insert(int index,
KType e1)
Inserts the specified element at the specified position in this list.
|
boolean |
isEmpty()
Shortcut for
size() == 0 . |
java.util.Iterator<KTypeCursor<KType>> |
iterator()
Returns an iterator to a cursor traversing the collection.
|
int |
lastIndexOf(KType e1)
Returns the index of the last occurrence of the specified element in this list,
or -1 if this list does not contain the element.
|
static <KType> KTypeArrayList<KType> |
newInstance()
Returns a new object of this class with no need to declare generic type (shortcut
instead of using a constructor).
|
static <KType> KTypeArrayList<KType> |
newInstanceWithCapacity(int initialCapacity)
Returns a new object of this class with no need to declare generic type (shortcut
instead of using a constructor).
|
void |
release()
Sets the number of stored elements to zero and releases the internal storage array.
|
KType |
remove(int index)
Removes the element at the specified position in this list and returns it.
|
int |
removeAll(KTypeLookupContainer<? extends KType> c)
Default implementation uses a predicate for removal.
|
int |
removeAll(KTypePredicate<? super KType> predicate)
Removes all elements in this collection for which the
given predicate returns
true . |
int |
removeAllOccurrences(KType e1)
Removes all occurrences of
e from this collection. |
int |
removeFirstOccurrence(KType e1)
Removes the first element that equals
e1 , returning its
deleted position or -1 if the element was not found. |
int |
removeLastOccurrence(KType e1)
Removes the last element that equals
e1 , returning its
deleted position or -1 if the element was not found. |
void |
removeRange(int fromIndex,
int toIndex)
Removes from this list all of the elements whose index is between
fromIndex , inclusive, and toIndex , exclusive. |
void |
resize(int newSize)
Truncate or expand the list to the new size.
|
int |
retainAll(KTypeLookupContainer<? extends KType> c)
Default implementation uses a predicate for retaining.
|
int |
retainAll(KTypePredicate<? super KType> predicate)
Default implementation redirects to
KTypeCollection.removeAll(KTypePredicate)
and negates the predicate. |
KType |
set(int index,
KType e1)
Replaces the element at the specified position in this list
with the specified element.
|
int |
size()
Return the current number of elements in this container.
|
java.lang.Object[] |
toArray()
Copies all elements from this container to an Object array.
|
KType[] |
toArray(java.lang.Class<? super KType> clazz)
Default implementation of copying to an array.
|
java.lang.String |
toString()
Convert the contents of this container to a human-friendly string.
|
void |
trimToSize()
Trim the internal buffer to the current size.
|
finalize, getClass, notify, notifyAll, wait, wait, wait
removeAll, retainAll, retainAll
toArray
public static final int DEFAULT_CAPACITY
public KType[] buffer
size()
).
#if ($TemplateOptions.KTypeGeneric)
The actual value in this field is always an instance of Object[]
,
regardless of the generic type used. The JDK is inconsistent here too --
ArrayList
declares internal Object[]
buffer, but
ArrayDeque
declares an array of generic type objects like we do. The
tradeoff is probably minimal, but you should be aware of additional casts generated
by javac
when buffer
is directly accessed - these casts
may result in exceptions at runtime. A workaround is to cast directly to
Object[]
before accessing the buffer's elements.#end
public int elementsCount
buffer
.protected final ArraySizingStrategy resizer
public KTypeArrayList()
BoundedProportionalArraySizingStrategy
public KTypeArrayList(int initialCapacity)
BoundedProportionalArraySizingStrategy
public KTypeArrayList(int initialCapacity, ArraySizingStrategy resizer)
public KTypeArrayList(KTypeContainer<? extends KType> container)
public void add(KType e1)
add
in interface KTypeIndexedContainer<KType>
public void add(KType e1, KType e2)
add
(vararg-version) or access the buffer directly (tight
loop).public void add(KType[] elements, int start, int length)
public void add(KType... elements)
This method is handy, but costly if used in tight loops (anonymous array passing)
public int addAll(KTypeContainer<? extends KType> container)
public int addAll(java.lang.Iterable<? extends KTypeCursor<? extends KType>> iterable)
public void insert(int index, KType e1)
insert
in interface KTypeIndexedContainer<KType>
index
- The index at which the element should be inserted, shifting
any existing and subsequent elements to the right.public KType get(int index)
get
in interface KTypeIndexedContainer<KType>
index
from the list.public KType set(int index, KType e1)
set
in interface KTypeIndexedContainer<KType>
public KType remove(int index)
Careful. Do not confuse this method with the overridden signature in
Java Collections (List.remove(Object)
). Use: KTypeCollection.removeAll(com.carrotsearch.hppc.KTypeLookupContainer<? extends KType>)
,
KTypeIndexedContainer.removeFirstOccurrence(KType)
or KTypeIndexedContainer.removeLastOccurrence(KType)
depending
on the actual need.
remove
in interface KTypeIndexedContainer<KType>
public void removeRange(int fromIndex, int toIndex)
fromIndex
, inclusive, and toIndex
, exclusive.removeRange
in interface KTypeIndexedContainer<KType>
public int removeFirstOccurrence(KType e1)
e1
, returning its
deleted position or -1
if the element was not found.removeFirstOccurrence
in interface KTypeIndexedContainer<KType>
public int removeLastOccurrence(KType e1)
e1
, returning its
deleted position or -1
if the element was not found.removeLastOccurrence
in interface KTypeIndexedContainer<KType>
public int removeAllOccurrences(KType e1)
e
from this collection.removeAllOccurrences
in interface KTypeCollection<KType>
e1
- Element to be removed from this collection, if present.public boolean contains(KType e1)
contains
in interface KTypeContainer<KType>
true
if this container has an element
equal to e
.public int indexOf(KType e1)
indexOf
in interface KTypeIndexedContainer<KType>
public int lastIndexOf(KType e1)
lastIndexOf
in interface KTypeIndexedContainer<KType>
public boolean isEmpty()
size() == 0
.isEmpty
in interface KTypeContainer<KType>
public void ensureCapacity(int minCapacity)
protected void ensureBufferSpace(int expectedAdditions)
expectedAdditions
. Increases internal buffer size if needed.public void resize(int newSize)
trimToSize()
if you need a truncated buffer),
but the truncated values will be reset to the default value (zero). If the list is
expanded, the elements beyond the current size are initialized with JVM-defaults
(zero or null
values).public int size()
O(n)
time, although implementing classes
should try to maintain the current size and return in constant time.size
in interface KTypeContainer<KType>
public void trimToSize()
public void clear()
elementsCount
field to zero.clear
in interface KTypeCollection<KType>
public void release()
public java.lang.Object[] toArray()
KTypeContainer.toArray(Class)
.
The returned array is sized to match exactly the number of elements of the stack.
toArray
in interface KTypeContainer<KType>
KTypeContainer.toArray(Class)
public KTypeArrayList<KType> clone()
clone
in class java.lang.Object
public int hashCode()
hashCode
in interface KTypeIndexedContainer<KType>
hashCode
in class java.lang.Object
List.hashCode()
(should be implemented
with the same algorithm).public boolean equals(java.lang.Object obj)
KTypeIndexedContainer
, both have the same size, and all corresponding
pairs of elements at the same index are equal. In other words, two indexed
containers are defined to be equal if they contain the same elements in the same
order.
Note that, unlike in List
, containers may be of different types and still
return true
from KTypeIndexedContainer.equals(java.lang.Object)
. This may be dangerous if you use
different hash functions in two containers, but don't override the default
implementation of KTypeIndexedContainer.equals(java.lang.Object)
. It is the programmer's responsibility to
enforcing these contracts properly.
equals
in interface KTypeIndexedContainer<KType>
equals
in class java.lang.Object
public java.util.Iterator<KTypeCursor<KType>> iterator()
The iterator is implemented as a
cursor and it returns the same cursor instance on every call to
Iterator.next()
(to avoid boxing of primitive types). To read the current
list's value (or index in the list) use the cursor's public fields. An example is
shown below.
for (KTypeCursor<KType> c : container) { System.out.println("index=" + c.index + " value=" + c.value); }
iterator
in interface KTypeContainer<KType>
iterator
in interface java.lang.Iterable<KTypeCursor<KType>>
public <T extends KTypeProcedure<? super KType>> T forEach(T procedure)
procedure
to all container elements. Returns the argument (any
subclass of KTypeProcedure
. This lets the caller to call methods of the argument
by chaining the call (even if the argument is an anonymous type) to retrieve computed values,
for example (IntContainer):
int count = container.forEach(new IntProcedure() { int count; // this is a field declaration in an anonymous class. public void apply(int value) { count++; }}).count;
forEach
in interface KTypeContainer<KType>
public <T extends KTypeProcedure<? super KType>> T forEach(T procedure, int fromIndex, int toIndex)
procedure
to a slice of the list,
fromIndex
, inclusive, to toIndex
,
exclusive.public int removeAll(KTypePredicate<? super KType> predicate)
true
.removeAll
in interface KTypeCollection<KType>
public <T extends KTypePredicate<? super KType>> T forEach(T predicate)
predicate
to container elements as long, as the predicate
returns true
. The iteration is interrupted otherwise.forEach
in interface KTypeContainer<KType>
public <T extends KTypePredicate<? super KType>> T forEach(T predicate, int fromIndex, int toIndex)
predicate
to a slice of the list,
fromIndex
, inclusive, to toIndex
,
exclusive, or until predicate returns false
.public static <KType> KTypeArrayList<KType> newInstance()
public static <KType> KTypeArrayList<KType> newInstanceWithCapacity(int initialCapacity)
public static <KType> KTypeArrayList<KType> from(KType... elements)
KType
.
The elements are copied from the argument to the internal buffer.public static <KType> KTypeArrayList<KType> from(KTypeContainer<KType> container)
public int removeAll(KTypeLookupContainer<? extends KType> c)
removeAll
in interface KTypeCollection<KType>
public int retainAll(KTypeLookupContainer<? extends KType> c)
retainAll
in interface KTypeCollection<KType>
public int retainAll(KTypePredicate<? super KType> predicate)
KTypeCollection.removeAll(KTypePredicate)
and negates the predicate.retainAll
in interface KTypeCollection<KType>
public KType[] toArray(java.lang.Class<? super KType> clazz)
toArray
in interface KTypeContainer<KType>
public java.lang.String toString()
toString
in class java.lang.Object
Copyright © 2014 Carrot Search s.c.. All rights reserved.