Fourth chapter Copyonwritearrayset Source code Analysis

Source: Internet
Author: User

Note: Before looking at this article, if the bottom of the copyonwritearraylist is not clear, it is recommended to first look at Copyonwritearraylist source parsing.

Http://www.cnblogs.com/java-zhao/p/5121944.html

1, for Copyonwritearrayset need to master the following points

    • Created: Copyonwritearrayset ()
    • add element: That is, the Add (E) method
    • Delete object: Remove (E) method
    • Traversing all objects: iterator (), which is more commonly used in practice is the enhanced for loop to do traversal

Note:

    • Copyonwritearrayset (Cannot add duplicate elements) the underlying is copyonwritearraylist (can add repeating elements).
    • The set collection does not get or modify or add or remove methods directly by index (eg.get (int index), add (int index,e e), set (int index,e e), remove (int index))

2. Create

Public Copyonwritearrayset ()

How to use:

New Copyonwritearrayset<string> ();

Source:

    Private Final Copyonwritearraylist<e> al; // underlying data structure     Public Copyonwritearrayset () {        new copyonwritearraylist<e>();    }
View Code

Note the point:

    • The bottom of Copyonwritearrayset is a copyonwritearraylist.

3. Adding elements

Public boolean Add (E E)

How to use:

Strset.add ("Hello")

Source:

    /**      * Loop through the old array, if there is the same value as E, return false     * If not, to the     last interpolation */ public     Boolean  Add (e e) {        return  al.addifabsent (e);    }
View Code

Copyonwritearraylist's Addifabsent (e e)

     Public Booleanaddifabsent (e e) {FinalReentrantlock lock = This. Lock;        Lock.lock (); Try{object[] elements=GetArray (); intLen =elements.length; Object[] Newelements=NewObject[len + 1];  for(inti = 0; i < Len; ++i) {if(EQ (E, elements[i]))//Loop through it first to see if there's a value that's the same value you want to insert.                    return false;//If there is, return directly                ElseNewelements[i]=Elements[i]; } Newelements[len]= e;//if not, assign a valueSetArray (newelements); return true; } finally{lock.unlock (); }    }
View Code

Note: This together source code is very simple, as long as you read the Copyonwritearraylist source parsing in the Add method can read

Note the point:

    • Copyonwritearrayset every time the add has to go through the group, performance is lower than copyonwritearraylist

4. Deleting elements

public boolean remove (Object o)

How to use:

Strset.remove ("Hello")

Source:

    /**      * Call Copyonwritearraylist's remove (Object O) method     */ public     boolean Remove (Object o) {        return  al.remove (o);    }
View Code

5. Traverse all elements

Public iterator<e> Iterator ()

How to use: See the previous chapter "Copyonwritearraylist Source Code Analysis"

Source:

    /**      * Call Copyonwritearraylist's Iterator ()     */public     iterator<e>  Iterator () {        return  al.iterator ();    }
View Code

The remaining source code see the previous chapter "Copyonwritearraylist Source Code Analysis"

Summarize:

    • The bottom of Copyonwritearrayset is a copyonwritearraylist.
    • Copyonwritearrayset in the add element to iterate through the array, so as not to add duplicate elements of the role, but because to go through the group, the efficiency will be lower than the Copyonwritearraylist add
    • The set collection does not get or modify or add or remove methods directly by index (eg.get (int index), add (int index,e e), set (int index,e e), remove (int index))

Fourth chapter Copyonwritearrayset Source code Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.