public class ReadMostlyVector<E> extends Object implements List<E>, RandomAccess, Cloneable, Serializable
Vector but with reduced contention and improved
throughput when invocations of read-only methods by multiple
threads are most common.
The iterators returned by this class's iterator and listIterator methods are
best-effort in the presence of concurrent modifications, and do
NOT throw ConcurrentModificationException. An
iterator's next() method returns consecutive elements as
they appear in the underlying array upon each access. Alternatively,
method snapshotIterator() may be used for deterministic
traversals, at the expense of making a copy, and unavailability of
method Iterator.remove.
Otherwise, this class supports all methods, under the same
documented specifications, as Vector. Consult Vector for detailed specifications. Additionally, this
class provides methods addIfAbsent(E) and addAllAbsent(java.util.Collection<? extends E>).
| Constructor and Description |
|---|
ReadMostlyVector()
Creates an empty vector with an underlying array of size
10. |
ReadMostlyVector(Collection<? extends E> c)
Creates a vector containing the elements of the specified
collection, in the order they are returned by the collection's
iterator.
|
ReadMostlyVector(int initialCapacity)
Creates an empty vector with the given initial capacity.
|
ReadMostlyVector(int initialCapacity,
int capacityIncrement)
Creates an empty vector with the given initial capacity and
capacity increment.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E e)
Appends the specified element to the end of this list (optional
operation).
|
void |
add(int index,
E element)
Inserts the specified element at the specified position in this list
(optional operation).
|
boolean |
addAll(Collection<? extends E> c)
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 (optional operation).
|
boolean |
addAll(int index,
Collection<? extends E> c)
Inserts all of the elements in the specified collection into this
list at the specified position (optional operation).
|
int |
addAllAbsent(Collection<? extends E> c)
Appends all of the elements in the specified collection that
are not already contained in this list, to the end of
this list, in the order that they are returned by the
specified collection's iterator.
|
void |
addElement(E obj)
|
boolean |
addIfAbsent(E e)
Appends the element, if not present.
|
int |
capacity()
|
void |
clear()
Removes all of the elements from this list (optional operation).
|
ReadMostlyVector<E> |
clone()
Creates and returns a copy of this object.
|
boolean |
contains(Object o)
Returns true if this list contains the specified element.
|
boolean |
containsAll(Collection<?> c)
Returns true if this list contains all of the elements of the
specified collection.
|
void |
copyInto(Object[] anArray)
|
E |
elementAt(int index)
|
Enumeration<E> |
elements()
|
void |
ensureCapacity(int minCapacity)
|
boolean |
equals(Object o)
Indicates whether some other object is "equal to" this one.
|
E |
firstElement()
|
E |
get(int index)
Returns the element at the specified position in this list.
|
int |
hashCode()
Returns a hash code value for the object.
|
int |
indexOf(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.
|
int |
indexOf(Object o,
int index)
|
void |
insertElementAt(E obj,
int index)
|
boolean |
isEmpty()
Returns true if this list contains no elements.
|
Iterator<E> |
iterator()
Returns an iterator over the elements in this list in proper sequence.
|
E |
lastElement()
|
int |
lastIndexOf(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.
|
int |
lastIndexOf(Object o,
int index)
|
ListIterator<E> |
listIterator()
Returns a list iterator over the elements in this list (in proper
sequence).
|
ListIterator<E> |
listIterator(int index)
Returns a list iterator of the elements in this list (in proper
sequence), starting at the specified position in this list.
|
E |
remove(int index)
Removes the element at the specified position in this list (optional
operation).
|
boolean |
remove(Object o)
Removes the first occurrence of the specified element from this list,
if it is present (optional operation).
|
boolean |
removeAll(Collection<?> c)
Removes from this list all of its elements that are contained in the
specified collection (optional operation).
|
void |
removeAllElements()
|
boolean |
removeElement(Object obj)
|
void |
removeElementAt(int index)
|
boolean |
retainAll(Collection<?> c)
Retains only the elements in this list that are contained in the
specified collection (optional operation).
|
E |
set(int index,
E element)
Replaces the element at the specified position in this list with the
specified element (optional operation).
|
void |
setElementAt(E obj,
int index)
|
void |
setSize(int newSize)
|
int |
size()
Returns the number of elements in this list.
|
Iterator<E> |
snapshotIterator()
Returns an iterator operating over a snapshot copy of the
elements of this collection created upon construction of the
iterator.
|
List<E> |
subList(int fromIndex,
int toIndex)
Returns a view of the portion of this list between the specified
fromIndex, inclusive, and toIndex, exclusive.
|
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[] a)
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.
|
String |
toString()
Returns a string representation of the object.
|
void |
trimToSize()
|
public ReadMostlyVector(int initialCapacity,
int capacityIncrement)
initialCapacity - the initial capacity of the underlying arraycapacityIncrement - if non-zero, the number to
add when resizing to accommodate additional elements.
If zero, the array size is doubled when resized.IllegalArgumentException - if initial capacity is negativepublic ReadMostlyVector(int initialCapacity)
initialCapacity - the initial capacity of the underlying arrayIllegalArgumentException - if initial capacity is negativepublic ReadMostlyVector()
10.public ReadMostlyVector(Collection<? extends E> c)
c - the collection of initially held elementsNullPointerException - if the specified collection is nullpublic boolean add(E e)
java.util.ListLists that support this operation may place limitations on what elements may be added to this list. In particular, some lists will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. List classes should clearly specify in their documentation any restrictions on what elements may be added.
add in interface Collection<E>add in interface List<E>e - element to be appended to this listCollection.add(E))public void add(int index,
E element)
java.util.Listpublic boolean addAll(Collection<? extends E> c)
java.util.ListaddAll in interface Collection<E>addAll in interface List<E>c - collection containing elements to be added to this listList.add(Object)public boolean addAll(int index,
Collection<? extends E> c)
java.util.Listpublic void clear()
java.util.Listpublic boolean contains(Object o)
java.util.Listpublic boolean containsAll(Collection<?> c)
java.util.ListcontainsAll in interface Collection<E>containsAll in interface List<E>c - collection to be checked for containment in this listList.contains(Object)public boolean equals(Object o)
java.lang.Object
The equals method implements an equivalence relation
on non-null object references:
x, x.equals(x) should return
true.
x and y, x.equals(y)
should return true if and only if
y.equals(x) returns true.
x, y, and z, if
x.equals(y) returns true and
y.equals(z) returns true, then
x.equals(z) should return true.
x and y, multiple invocations of
x.equals(y) consistently return true
or consistently return false, provided no
information used in equals comparisons on the
objects is modified.
x,
x.equals(null) should return false.
The equals method for class Object implements
the most discriminating possible equivalence relation on objects;
that is, for any non-null reference values x and
y, this method returns true if and only
if x and y refer to the same object
(x == y has the value true).
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
equals in interface Collection<E>equals in interface List<E>equals in class Objecto - the reference object with which to compare.true if this object is the same as the obj
argument; false otherwise.Object.hashCode(),
Hashtablepublic E get(int index)
java.util.Listpublic int hashCode()
java.lang.Objectjava.util.Hashtable.
The general contract of hashCode is:
hashCode method on each of
the two objects must produce the same integer result.
Object.equals(java.lang.Object)
method, then calling the hashCode method on each of the
two objects must produce distinct integer results. However, the
programmer should be aware that producing distinct integer results
for unequal objects may improve the performance of hashtables.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
hashCode in interface Collection<E>hashCode in interface List<E>hashCode in class ObjectObject.equals(java.lang.Object),
Hashtablepublic int indexOf(Object o)
java.util.Listpublic boolean isEmpty()
java.util.Listpublic Iterator<E> iterator()
java.util.Listpublic int lastIndexOf(Object o)
java.util.ListlastIndexOf in interface List<E>o - element to search forpublic ListIterator<E> listIterator()
java.util.ListlistIterator in interface List<E>public ListIterator<E> listIterator(int index)
java.util.Listnext.
An initial call to previous would
return the element with the specified index minus one.listIterator in interface List<E>index - index of first element to be returned from the
list iterator (by a call to the next method)public E remove(int index)
java.util.Listpublic boolean remove(Object o)
java.util.Listpublic boolean removeAll(Collection<?> c)
java.util.ListremoveAll in interface Collection<E>removeAll in interface List<E>c - collection containing elements to be removed from this listList.remove(Object),
List.contains(Object)public boolean retainAll(Collection<?> c)
java.util.ListretainAll in interface Collection<E>retainAll in interface List<E>c - collection containing elements to be retained in this listList.remove(Object),
List.contains(Object)public E set(int index, E element)
java.util.Listpublic int size()
java.util.Listpublic List<E> subList(int fromIndex, int toIndex)
java.util.ListThis method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:
list.subList(from, to).clear();
Similar idioms may be constructed for indexOf and
lastIndexOf, and all of the algorithms in the
Collections class can be applied to a subList.The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)
public Object[] toArray()
java.util.ListThe returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array even if this list is backed by an array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
toArray in interface Collection<E>toArray in interface List<E>Arrays.asList(Object[])public <T> T[] toArray(T[] a)
java.util.ListIf the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)
Like the List.toArray() method, this method acts as bridge between
array-based and collection-based APIs. Further, this method allows
precise control over the runtime type of the output array, and may,
under certain circumstances, be used to save allocation costs.
Suppose x is a list known to contain only strings. The following code can be used to dump the list into a newly allocated array of String:
String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to
toArray().toArray in interface Collection<E>toArray in interface List<E>a - the array into which the elements of this list are to
be stored, if it is big enough; otherwise, a new array of the
same runtime type is allocated for this purpose.public String toString()
java.lang.ObjecttoString method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
public boolean addIfAbsent(E e)
e - element to be added to this list, if absenttrue if the element was addedpublic int addAllAbsent(Collection<? extends E> c)
c - collection containing elements to be added to this listNullPointerException - if the specified collection is nulladdIfAbsent(Object)public Iterator<E> snapshotIterator()
remove method.public E firstElement()
public E lastElement()
public int indexOf(Object o, int index)
public int lastIndexOf(Object o, int index)
public void setSize(int newSize)
public void copyInto(Object[] anArray)
public void trimToSize()
public void ensureCapacity(int minCapacity)
public Enumeration<E> elements()
public int capacity()
public E elementAt(int index)
public void setElementAt(E obj, int index)
public void removeElementAt(int index)
public void insertElementAt(E obj, int index)
public void addElement(E obj)
public boolean removeElement(Object obj)
public void removeAllElements()
public ReadMostlyVector<E> clone()
java.lang.Objectwill be true, and that the expression:x.clone() != x
will be true, but these are not absolute requirements. While it is typically the case that:x.clone().getClass() == x.getClass()
will be true, this is not an absolute requirement.x.clone().equals(x)
By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().
By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.
The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. Note that all arrays are considered to implement the interface Cloneable. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.
The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time.