Chapter 4 CopyOnWriteArraySet source code parsing: copyonwritearrayset
Note: Before reading this article, if you are not clear about the bottom layer of CopyOnWriteArrayList, we recommend that you first check the source code parsing of CopyOnWriteArrayList.
Http://www.cnblogs.com/java-zhao/p/5121944.html
1. Master the following points for CopyOnWriteArraySet:
- Create: CopyOnWriteArraySet ()
- Add element: add (E) Method
- Delete object: remove (E) Method
- Traverse all objects: iterator (). In practice, an enhanced for loop is more commonly used for traversal.
Note:
- The bottom layer of CopyOnWriteArraySet (repeated elements cannot be added) isCopyOnWriteArrayList (repeated elements can be added).
- The Set does not directly obtain, modify, add, or delete the Set by index (eg. get (int index), add (int index, E e), set (int index, E e), remove (int index ))
2. Create
Public CopyOnWriteArraySet ()
Usage:
Set<String> strSet = new CopyOnWriteArraySet<String>();
Source code:
Private final CopyOnWriteArrayList <E> al; // underlying data structure public CopyOnWriteArraySet () {al = new CopyOnWriteArrayList <E> ();}View Code
Note:
- The bottom layer of the CopyOnWriteArraySet is a CopyOnWriteArrayList.
3. Add Elements
Public boolean add (E e)
Usage:
strSet.add("hello")
Source code:
/*** Traverse the old array cyclically. If there is a value the same as e, return false * If not, interpolation to the last */public boolean add (E e) {return al. addIfAbsent (e );}View Code
AddIfAbsent (E e) of CopyOnWriteArrayList)
Public boolean addIfAbsent (E e) {final ReentrantLock lock = this. lock; lock. lock (); try {Object [] elements = getArray (); int len = elements. length; Object [] newElements = new Object [len + 1]; for (int I = 0; I <len; ++ I) {if (eq (e, elements [I]) // loop through to see if there is any return false value that is the same as the value to be inserted; // If yes, directly return else newElements [I] = elements [I];} newElements [len] = e; // if not, assign setArray (newElements); return true ;} finally {lock. unlock ();}}View Code
Note: the source code is very simple, as long as you read the add method in CopyOnWriteArrayList source code parsing, you can understand it.
Note:
- The CopyOnWriteArraySet traverses the array every time it is added. The performance is lower than that of CopyOnWriteArrayList.
4. Delete Elements
Public boolean remove (Object o)
Usage:
strSet.remove("hello")
Source code:
/*** Call the remove (Object o) method of CopyOnWriteArrayList */public boolean remove (Object o) {return al. remove (o );}View Code
5. traverse all elements
Public Iterator <E> iterator ()
Usage: see CopyOnWriteArrayList source code analysis in the previous chapter.
Source code:
/*** Call CopyOnWriteArrayList iterator () */public Iterator <E> iterator () {return al. iterator ();}View Code
For the remaining source code, see the previous chapter CopyOnWriteArrayList source code parsing.
Summary:
- The bottom layer of the CopyOnWriteArraySet is a CopyOnWriteArrayList.
- CopyOnWriteArraySet traverses the array when adding an element, so as not to add repeated elements. However, to traverse the array, the efficiency is lower than the add of CopyOnWriteArrayList.
- The Set does not directly obtain, modify, add, or delete the Set by index (eg. get (int index), add (int index, E e), set (int index, E e), remove (int index ))