Copy on write arraylist java

Copy on write arraylist java

A thread-safe variant of ArrayList in which all mutative operations ( add , set , and so on) are implemented by making a fresh copy of the underlying array. This is ordinarily too costly, but may be more efficient than alternatives when traversal operations vastly outnumber mutations, and is useful when you cannot or don’t want to synchronize traversals, yet need to preclude interference among concurrent threads. The «snapshot» style iterator method uses a reference to the state of the array at the point that the iterator was created. This array never changes during the lifetime of the iterator, so interference is impossible and the iterator is guaranteed not to throw ConcurrentModificationException . The iterator will not reflect additions, removals, or changes to the list since the iterator was created. Element-changing operations on iterators themselves ( remove , set , and add ) are not supported. These methods throw UnsupportedOperationException . All elements are permitted, including null . Memory consistency effects: As with other concurrent collections, actions in a thread prior to placing an object into a CopyOnWriteArrayList happen-before actions subsequent to the access or removal of that element from the CopyOnWriteArrayList in another thread. This class is a member of the Java Collections Framework.

Constructor Summary

Creates a list containing the elements of the specified collection, in the order they are returned by the collection’s iterator.

Читайте также:  If else case in python

Method Summary

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.

Inserts all of the elements in the specified collection into this list, starting at the specified position.

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.

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

Returns the index of the first occurrence of the specified element in this list, searching forwards from index , or returns -1 if the element is not found.

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

Returns the index of the last occurrence of the specified element in this list, searching backwards from index , or returns -1 if the element is not found.

Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.

Returns an array containing all of the elements in this list in proper sequence (from first to last element).

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.

Источник

Class CopyOnWriteArrayList

A thread-safe variant of ArrayList in which all mutative operations ( add , set , and so on) are implemented by making a fresh copy of the underlying array.

This is ordinarily too costly, but may be more efficient than alternatives when traversal operations vastly outnumber mutations, and is useful when you cannot or don’t want to synchronize traversals, yet need to preclude interference among concurrent threads. The «snapshot» style iterator method uses a reference to the state of the array at the point that the iterator was created. This array never changes during the lifetime of the iterator, so interference is impossible and the iterator is guaranteed not to throw ConcurrentModificationException . The iterator will not reflect additions, removals, or changes to the list since the iterator was created. Element-changing operations on iterators themselves ( remove , set , and add ) are not supported. These methods throw UnsupportedOperationException .

All elements are permitted, including null .

Memory consistency effects: As with other concurrent collections, actions in a thread prior to placing an object into a CopyOnWriteArrayList happen-before actions subsequent to the access or removal of that element from the CopyOnWriteArrayList in another thread.

This class is a member of the Java Collections Framework.

Constructor Summary

Creates a list containing the elements of the specified collection, in the order they are returned by the collection’s iterator.

Method Summary

Inserts all of the elements in the specified collection into this list, starting at the specified position.

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.

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.

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

Returns the index of the first occurrence of the specified element in this list, searching forwards from index , or returns -1 if the element is not found.

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

Returns the index of the last occurrence of the specified element in this list, searching backwards from index , or returns -1 if the element is not found.

Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.

Returns an array containing all of the elements in this list in proper sequence (from first to last element).

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.

Methods declared in class java.lang.Object

Methods declared in interface java.util.Collection

Methods declared in interface java.util.List

Constructor Details

CopyOnWriteArrayList

CopyOnWriteArrayList

Creates a list containing the elements of the specified collection, in the order they are returned by the collection’s iterator.

CopyOnWriteArrayList

Method Details

size

isEmpty

contains

Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that Objects.equals(o, e) .

indexOf

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that Objects.equals(o, get(i)) , or -1 if there is no such index.

indexOf

Returns the index of the first occurrence of the specified element in this list, searching forwards from index , or returns -1 if the element is not found. More formally, returns the lowest index i such that i >= index && Objects.equals(get(i), e) , or -1 if there is no such index.

lastIndexOf

Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the highest index i such that Objects.equals(o, get(i)) , or -1 if there is no such index.

lastIndexOf

Returns the index of the last occurrence of the specified element in this list, searching backwards from index , or returns -1 if the element is not found. More formally, returns the highest index i such that i

clone

toArray

Returns an array containing all of the elements in this list in proper sequence (from first to last element). The 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). The caller is thus free to modify the returned array. This method acts as bridge between array-based and collection-based APIs.

toArray

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. 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. If this list fits in the specified array with room to spare (i.e., the array has more elements than this 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 this list only if the caller knows that this list does not contain any null elements.) Like the 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]);

Источник

Copy on write arraylist java

A thread-safe variant of ArrayList in which all mutative operations ( add , set , and so on) are implemented by making a fresh copy of the underlying array. This is ordinarily too costly, but may be more efficient than alternatives when traversal operations vastly outnumber mutations, and is useful when you cannot or don’t want to synchronize traversals, yet need to preclude interference among concurrent threads. The «snapshot» style iterator method uses a reference to the state of the array at the point that the iterator was created. This array never changes during the lifetime of the iterator, so interference is impossible and the iterator is guaranteed not to throw ConcurrentModificationException . The iterator will not reflect additions, removals, or changes to the list since the iterator was created. Element-changing operations on iterators themselves ( remove , set , and add ) are not supported. These methods throw UnsupportedOperationException . All elements are permitted, including null . Memory consistency effects: As with other concurrent collections, actions in a thread prior to placing an object into a CopyOnWriteArrayList happen-before actions subsequent to the access or removal of that element from the CopyOnWriteArrayList in another thread. This class is a member of the Java Collections Framework.

Constructor Summary

Creates a list containing the elements of the specified collection, in the order they are returned by the collection’s iterator.

Method Summary

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.

Inserts all of the elements in the specified collection into this list, starting at the specified position.

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.

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

Returns the index of the first occurrence of the specified element in this list, searching forwards from index , or returns -1 if the element is not found.

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

Returns the index of the last occurrence of the specified element in this list, searching backwards from index , or returns -1 if the element is not found.

Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.

Returns an array containing all of the elements in this list in proper sequence (from first to last element).

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.

Источник

Оцените статью