Class FastArrayList
- All Implemented Interfaces:
Serializable,Cloneable,Iterable,Collection,List,RandomAccess
A customized implementation of java.util.ArrayList designed
to operate in a multithreaded environment where the large majority of
method calls are read-only, instead of structural changes. When operating
in "fast" mode, read calls are non-synchronized and write calls perform the
following steps:
- Clone the existing collection
- Perform the modification on the clone
- Replace the existing collection with the (modified) clone
When first created, objects of this class default to "slow" mode, where
all accesses of any type are synchronized but no cloning takes place. This
is appropriate for initially populating the collection, followed by a switch
to "fast" mode (by calling setFast(true)) after initialization
is complete.
NOTE: If you are creating and accessing an
ArrayList only within a single thread, you should use
java.util.ArrayList directly (with no synchronization), for
maximum performance.
NOTE: This class is not cross-platform. Using it may cause unexpected failures on some architectures. It suffers from the same problems as the double-checked locking idiom. In particular, the instruction that clones the internal collection and the instruction that sets the internal reference to the clone can be executed or perceived out-of-order. This means that any read operation might fail unexpectedly, as it may be reading the state of the internal collection before the internal collection is fully formed. For more information on the double-checked locking idiom, see the Double-Checked Locking Idiom Is Broken Declaration.
- Since:
- Commons Collections 1.0
- Version:
- $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
- Author:
- Craig R. McClanahan, Stephen Colebourne
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanAre we operating in "fast" mode?protected ArrayListThe underlying list we are managing.Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionConstruct a an empty list.FastArrayList(int capacity) Construct an empty list with the specified capacity.FastArrayList(Collection collection) Construct a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. -
Method Summary
Modifier and TypeMethodDescriptionvoidInsert the specified element at the specified position in this list, and shift all remaining elements up one position.booleanAppends the specified element to the end of this list.booleanaddAll(int index, Collection collection) Insert all of the elements in the specified Collection at the specified position in this list, and shift any previous elements upwards as needed.booleanaddAll(Collection collection) Append 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.voidclear()Remove all of the elements from this list.clone()Return a shallow copy of thisFastArrayListinstance.booleanReturntrueif this list contains the specified element.booleancontainsAll(Collection collection) Returntrueif this list contains all of the elements in the specified Collection.voidensureCapacity(int capacity) Increase the capacity of thisArrayListinstance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.booleanCompare the specified object with this list for equality.get(int index) Return the element at the specified position in the list.booleangetFast()Returns true if this list is operating in fast mode.inthashCode()Return the hash code value for this list.intSearch for the first occurrence of the given argument, testing for equality using theequals()method, and return the corresponding index, or -1 if the object is not found.booleanisEmpty()Test if this list has no elements.iterator()Return an iterator over the elements in this list in proper sequence.intlastIndexOf(Object element) Search for the last occurrence of the given argument, testing for equality using theequals()method, and return the corresponding index, or -1 if the object is not found.Return an iterator of the elements of this list, in proper sequence.listIterator(int index) Return an iterator of the elements of this list, in proper sequence, starting at the specified position.remove(int index) Remove the element at the specified position in the list, and shift any subsequent elements down one position.booleanRemove the first occurrence of the specified element from the list, and shift any subsequent elements down one position.booleanremoveAll(Collection collection) Remove from this collection all of its elements that are contained in the specified collection.booleanretainAll(Collection collection) Remove from this collection all of its elements except those that are contained in the specified collection.Replace the element at the specified position in this list with the specified element.voidsetFast(boolean fast) Sets whether this list will operate in fast mode.intsize()Return the number of elements in this list.subList(int fromIndex, int toIndex) Return a view of the portion of this list between fromIndex (inclusive) and toIndex (exclusive).Object[]toArray()Return an array containing all of the elements in this list in the correct order.Object[]Return an array containing all of the elements in this list in the correct order.toString()Return a String representation of this object.voidTrim the capacity of thisArrayListinstance to be the list's current size.Methods inherited from class java.util.ArrayList
forEach, removeIf, removeRange, replaceAll, sort, spliteratorMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, stream, toArray
-
Field Details
-
list
The underlying list we are managing. -
fast
protected boolean fastAre we operating in "fast" mode?
-
-
Constructor Details
-
FastArrayList
public FastArrayList()Construct a an empty list. -
FastArrayList
public FastArrayList(int capacity) Construct an empty list with the specified capacity.- Parameters:
capacity- The initial capacity of the empty list
-
FastArrayList
Construct a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.- Parameters:
collection- The collection whose elements initialize the contents of this list
-
-
Method Details
-
getFast
public boolean getFast()Returns true if this list is operating in fast mode.- Returns:
- true if this list is operating in fast mode
-
setFast
public void setFast(boolean fast) Sets whether this list will operate in fast mode.- Parameters:
fast- true if the list should operate in fast mode
-
add
Appends the specified element to the end of this list. -
add
Insert the specified element at the specified position in this list, and shift all remaining elements up one position.- Specified by:
addin interfaceList- Overrides:
addin classArrayList- Parameters:
index- Index at which to insert this elementelement- The element to be inserted- Throws:
IndexOutOfBoundsException- if the index is out of range
-
addAll
Append 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. -
addAll
Insert all of the elements in the specified Collection at the specified position in this list, and shift any previous elements upwards as needed.- Specified by:
addAllin interfaceList- Overrides:
addAllin classArrayList- Parameters:
index- Index at which insertion takes placecollection- The collection to be added- Throws:
IndexOutOfBoundsException- if the index is out of range
-
clear
public void clear()Remove all of the elements from this list. The list will be empty after this call returns.- Specified by:
clearin interfaceCollection- Specified by:
clearin interfaceList- Overrides:
clearin classArrayList- Throws:
UnsupportedOperationException- ifclear()is not supported by this list
-
clone
Return a shallow copy of thisFastArrayListinstance. The elements themselves are not copied. -
contains
Returntrueif this list contains the specified element. -
containsAll
Returntrueif this list contains all of the elements in the specified Collection.- Specified by:
containsAllin interfaceCollection- Specified by:
containsAllin interfaceList- Overrides:
containsAllin classAbstractCollection- Parameters:
collection- Collection whose elements are to be checked
-
ensureCapacity
public void ensureCapacity(int capacity) Increase the capacity of thisArrayListinstance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.- Overrides:
ensureCapacityin classArrayList- Parameters:
capacity- The new minimum capacity
-
equals
Compare the specified object with this list for equality. This implementation uses exactly the code that is used to define the list equals function in the documentation for theList.equalsmethod. -
get
Return the element at the specified position in the list.- Specified by:
getin interfaceList- Overrides:
getin classArrayList- Parameters:
index- The index of the element to return- Throws:
IndexOutOfBoundsException- if the index is out of range
-
hashCode
public int hashCode()Return the hash code value for this list. This implementation uses exactly the code that is used to define the list hash function in the documentation for theList.hashCodemethod. -
indexOf
Search for the first occurrence of the given argument, testing for equality using theequals()method, and return the corresponding index, or -1 if the object is not found. -
isEmpty
public boolean isEmpty()Test if this list has no elements. -
iterator
Return an iterator over the elements in this list in proper sequence.Thread safety
The iterator returned is thread-safe ONLY in FAST mode. In slow mode there is no way to synchronize, or make the iterator thread-safe.In fast mode iteration and modification may occur in parallel on different threads, however there is a restriction. Modification must be EITHER via the Iterator interface methods OR the List interface. If a mixture of modification methods is used a ConcurrentModificationException is thrown from the iterator modification method. If the List modification methods are used the changes are NOT visible in the iterator (it shows the list contents at the time the iterator was created).
-
lastIndexOf
Search for the last occurrence of the given argument, testing for equality using theequals()method, and return the corresponding index, or -1 if the object is not found.- Specified by:
lastIndexOfin interfaceList- Overrides:
lastIndexOfin classArrayList- Parameters:
element- The element to search for
-
listIterator
Return an iterator of the elements of this list, in proper sequence.Thread safety
The iterator returned is thread-safe ONLY in FAST mode. In slow mode there is no way to synchronize, or make the iterator thread-safe.In fast mode iteration and modification may occur in parallel on different threads, however there is a restriction. Modification must be EITHER via the Iterator interface methods OR the List interface. If a mixture of modification methods is used a ConcurrentModificationException is thrown from the iterator modification method. If the List modification methods are used the changes are NOT visible in the iterator (it shows the list contents at the time the iterator was created).
- Specified by:
listIteratorin interfaceList- Overrides:
listIteratorin classArrayList- Returns:
- the list iterator
-
listIterator
Return an iterator of the elements of this list, in proper sequence, starting at the specified position.Thread safety
The iterator returned is thread-safe ONLY in FAST mode. In slow mode there is no way to synchronize, or make the iterator thread-safe.In fast mode iteration and modification may occur in parallel on different threads, however there is a restriction. Modification must be EITHER via the Iterator interface methods OR the List interface. If a mixture of modification methods is used a ConcurrentModificationException is thrown from the iterator modification method. If the List modification methods are used the changes are NOT visible in the iterator (it shows the list contents at the time the iterator was created).
- Specified by:
listIteratorin interfaceList- Overrides:
listIteratorin classArrayList- Parameters:
index- The starting position of the iterator to return- Returns:
- the list iterator
- Throws:
IndexOutOfBoundsException- if the index is out of range
-
remove
Remove the element at the specified position in the list, and shift any subsequent elements down one position.- Specified by:
removein interfaceList- Overrides:
removein classArrayList- Parameters:
index- Index of the element to be removed- Throws:
IndexOutOfBoundsException- if the index is out of range
-
remove
Remove the first occurrence of the specified element from the list, and shift any subsequent elements down one position. -
removeAll
Remove from this collection all of its elements that are contained in the specified collection.- Specified by:
removeAllin interfaceCollection- Specified by:
removeAllin interfaceList- Overrides:
removeAllin classArrayList- Parameters:
collection- Collection containing elements to be removed- Throws:
UnsupportedOperationException- if this optional operation is not supported by this list
-
retainAll
Remove from this collection all of its elements except those that are contained in the specified collection.- Specified by:
retainAllin interfaceCollection- Specified by:
retainAllin interfaceList- Overrides:
retainAllin classArrayList- Parameters:
collection- Collection containing elements to be retained- Throws:
UnsupportedOperationException- if this optional operation is not supported by this list
-
set
Replace the element at the specified position in this list with the specified element. Returns the previous object at that position.
IMPLEMENTATION NOTE - This operation is specifically documented to not be a structural change, so it is safe to be performed without cloning.- Specified by:
setin interfaceList- Overrides:
setin classArrayList- Parameters:
index- Index of the element to replaceelement- The new element to be stored- Throws:
IndexOutOfBoundsException- if the index is out of range
-
size
public int size()Return the number of elements in this list. -
subList
Return a view of the portion of this list between fromIndex (inclusive) and toIndex (exclusive). The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list. The returned list supports all of the optional list operations supported by this list.- Specified by:
subListin interfaceList- Overrides:
subListin classArrayList- Parameters:
fromIndex- The starting index of the sublist viewtoIndex- The index after the end of the sublist view- Throws:
IndexOutOfBoundsException- if an index is out of range
-
toArray
Return an array containing all of the elements in this list in the correct order. -
toArray
Return an array containing all of the elements in this list in the correct order. The runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array, and the size of this list.- Specified by:
toArrayin interfaceCollection- Specified by:
toArrayin interfaceList- Overrides:
toArrayin classArrayList- Parameters:
array- Array defining the element type of the returned list- Throws:
ArrayStoreException- if the runtime type ofarrayis not a supertype of the runtime type of every element in this list
-
toString
Return a String representation of this object.- Overrides:
toStringin classAbstractCollection
-
trimToSize
public void trimToSize()Trim the capacity of thisArrayListinstance to be the list's current size. An application can use this operation to minimize the storage of anArrayListinstance.- Overrides:
trimToSizein classArrayList
-