Package org.agrona.collections
Class IntArrayList
- All Implemented Interfaces:
Iterable<Integer>
,Collection<Integer>
,List<Integer>
,RandomAccess
,SequencedCollection<Integer>
A
List
implementation that stores int values with the ability to not have them boxed.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
The default value that will be used in place of null for an element.private int[]
static final int
Initial capacity to which the array will be sized.private final int
private int
Fields inherited from class java.util.AbstractList
modCount
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new list with theINITIAL_CAPACITY
usingDEFAULT_NULL_VALUE
.IntArrayList
(int[] initialElements, int initialSize, int nullValue) Create a new list that wraps an existing arrays without copying it.IntArrayList
(int initialCapacity, int nullValue) Construct a new list. -
Method Summary
Modifier and TypeMethodDescriptionvoid
boolean
boolean
addAll
(int index, IntArrayList list) Inserts all the elements from the specified list to this list at the specified position.boolean
addAll
(IntArrayList 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.boolean
addInt
(int element) Add an element without boxing.void
addInt
(int index, int element) Add an element without boxing at a given index.int
capacity()
The current capacity for the collection.private void
checkIndex
(int index) private void
checkIndexForAdd
(int index) void
clear()
boolean
boolean
containsAll
(IntArrayList list) Returnstrue
if this list contains all the elements of the specified list.boolean
containsInt
(int 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
boolean
equals
(IntArrayList that) Type-safe overload of theequals(Object)
method.int
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
fastUnorderedRemoveInt
(int value) Remove the first instance of a value if found in the list and replaces it with the last item in the list.void
void
forEachInt
(IntConsumer action) Iterate over the collection without boxing.void
forEachOrderedInt
(IntConsumer action) For each element in order provide the int value to aIntConsumer
.get
(int index) int
getInt
(int index) Get the element at a given index without boxing.int
hashCode()
int
indexOf
(int value) Index of the first element with this value.Create aIntStream
over the elements of underlying array.int
lastIndexOf
(int value) Index of the last element with this value.int
The value representing a null element.int
popInt()
Pop a value off the end of the array as a stack operation.void
pushInt
(int element) Push an element onto the end of the array like a stack.remove
(int index) Remove at a given index.boolean
boolean
removeAll
(IntArrayList list) Removes all of this collection's elements that are also contained in the specified list.int
removeAt
(int index) Remove at a given index.boolean
removeIfInt
(IntPredicate filter) Removes all the elements of this collection that satisfy the given predicate.boolean
removeInt
(int value) Remove the first instance of a value if found in the list.boolean
retainAll
(IntArrayList list) Retains only the elements in this list that are contained in the specified list.int
setInt
(int index, int element) Set an element at a given index without boxing.int
size()
int[]
Create a new array that is a copy of the elements.int[]
toIntArray
(int[] dst) Create a new array that is a copy of the elements.toString()
void
Trim the underlying array to be the current size, orINITIAL_CAPACITY
if size is less.void
wrap
(int[] 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, addFirst, addLast, containsAll, getFirst, getLast, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, removeAll, removeFirst, removeLast, replaceAll, retainAll, reversed, sort, spliterator, subList, toArray, toArray
-
Field Details
-
DEFAULT_NULL_VALUE
public static final int DEFAULT_NULL_VALUEThe default value that will be used in place of null for an element.- See Also:
-
INITIAL_CAPACITY
public static final int INITIAL_CAPACITYInitial capacity to which the array will be sized.- See Also:
-
nullValue
private final int nullValue -
size
private int size -
elements
private int[] elements
-
-
Constructor Details
-
IntArrayList
public IntArrayList()Constructs a new list with theINITIAL_CAPACITY
usingDEFAULT_NULL_VALUE
. -
IntArrayList
public IntArrayList(int initialCapacity, int nullValue) Construct a new list.- Parameters:
initialCapacity
- for the backing array.nullValue
- to be used to represent a null element.
-
IntArrayList
public IntArrayList(int[] initialElements, int initialSize, int 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 Details
-
wrap
public void wrap(int[] initialElements, int initialSize) Wrap an existing array without copying it.- Parameters:
initialElements
- to be wrapped.initialSize
- of the array to wrap.- Throws:
IllegalArgumentException
- if the initialSize is less than 0 or greater than the length of the initial array.
-
nullValue
public int nullValue()The value representing a null element.- Returns:
- value representing a null element.
-
size
public int size()- Specified by:
size
in interfaceCollection<Integer>
- Specified by:
size
in interfaceList<Integer>
- Specified by:
size
in classAbstractCollection<Integer>
-
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 interfaceCollection<Integer>
- Specified by:
clear
in interfaceList<Integer>
- Overrides:
clear
in classAbstractList<Integer>
-
trimToSize
public void trimToSize()Trim the underlying array to be the current size, orINITIAL_CAPACITY
if size is less. -
get
-
getInt
public int getInt(int index) Get the element at a given index without boxing.- Parameters:
index
- to get.- Returns:
- the unboxed element.
-
add
- Specified by:
add
in interfaceCollection<Integer>
- Specified by:
add
in interfaceList<Integer>
- Overrides:
add
in classAbstractList<Integer>
-
addInt
public boolean addInt(int element) Add an element without boxing.- Parameters:
element
- to be added.- Returns:
- true
-
add
-
addInt
public void addInt(int index, int element) Add an element without boxing at a given index.- Parameters:
index
- at which the element should be added.element
- to be added.
-
set
-
setInt
public int setInt(int index, int 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
- Specified by:
contains
in interfaceCollection<Integer>
- Specified by:
contains
in interfaceList<Integer>
- Overrides:
contains
in classAbstractCollection<Integer>
-
containsInt
public boolean containsInt(int value) Does the list contain this element value.- Parameters:
value
- of the element.- Returns:
- true if present otherwise false.
-
indexOf
public int indexOf(int 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(int value) Index of the last element with this value.- Parameters:
value
- for the element.- Returns:
- the index if found otherwise -1.
-
addAll
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
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
Returnstrue
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
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
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.
-
removeIfInt
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
- Specified by:
remove
in interfaceCollection<Integer>
- Specified by:
remove
in interfaceList<Integer>
- Overrides:
remove
in classAbstractCollection<Integer>
-
remove
Remove at a given index. -
removeAt
public int 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 int 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:
IndexOutOfBoundsException
- if index is out of bounds.
-
removeInt
public boolean removeInt(int 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.
-
fastUnorderedRemoveInt
public boolean fastUnorderedRemoveInt(int 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.
-
pushInt
public void pushInt(int element) Push an element onto the end of the array like a stack.- Parameters:
element
- to be pushed onto the end of the array.
-
popInt
public int popInt()Pop a value off the end of the array as a stack operation.- Returns:
- the value at the end of the array.
- Throws:
NoSuchElementException
- if the array is empty.
-
forEachOrderedInt
For each element in order provide the int value to aIntConsumer
.- Parameters:
action
- to be taken for each element.
-
intStream
Create aIntStream
over the elements of underlying array.- Returns:
- a
IntStream
over the elements of underlying array.
-
toIntArray
public int[] toIntArray()Create a new array that is a copy of the elements.- Returns:
- a copy of the elements.
-
toIntArray
public int[] toIntArray(int[] 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
Type-safe overload of theequals(Object)
method.- Parameters:
that
- other list.- Returns:
true
if lists are equal.
-
equals
- Specified by:
equals
in interfaceCollection<Integer>
- Specified by:
equals
in interfaceList<Integer>
- Overrides:
equals
in classAbstractList<Integer>
-
hashCode
public int hashCode()- Specified by:
hashCode
in interfaceCollection<Integer>
- Specified by:
hashCode
in interfaceList<Integer>
- Overrides:
hashCode
in classAbstractList<Integer>
-
forEach
-
forEachInt
Iterate over the collection without boxing.- Parameters:
action
- to be taken for each element.
-
toString
- Overrides:
toString
in classAbstractCollection<Integer>
-
ensureCapacityPrivate
private void ensureCapacityPrivate(int requiredCapacity) -
checkIndex
private void checkIndex(int index) -
checkIndexForAdd
private void checkIndexForAdd(int index)
-