Take you right to use the Retainall method of list to find the intersection

Source: Internet
Author: User

First, Retainall method
 Public boolean retainall (collection<?> c) {        // Call Own Private method        return  true);    }
Second, Batchremove method analysis

Returns True if this collection is changed because of a call

//Collection A compares the intersection with SET B     Private BooleanBatchremove (collection<?> C,Booleancomplement) {             //get all elements of the current object            Finalobject[] Elementdata = This. Elementdata; //W: Mark the number of common elements in two sets            intR = 0, W = 0; //set Flag bit            BooleanModified =false; Try {                //Traversing Collection A                 for(; r < size; r++)                    //determines whether the current element in collection A is contained in collection B                    if(C.contains (elementdata[r]) = =complement)//if it is included, it is saved directly. elementdata[w++] =Elementdata[r]; } finally {                //if C.contains () throws an exception                if(r! =size) {                    //Copy the remaining elementssystem.arraycopy (Elementdata, R, Elementdata, W, Size-R); //W is the length of the current set aW + = size-R; }                //if the size of set A is released to change                if(W! =size) {                    //Clear Work                     for(inti = W; i < size; i++) Elementdata[i]=NULL; //changes to elements in the record set (Add/remove)Modcount + = size-W; //sets the size of the current arraySize =W; //return to TrueModified =true; }            }            returnmodified; }

1, about the Modcount variable description

The abstractlist contains a modcount variable whose initial value is 0, and when the contents of the collection are modified every time (call Add (), remove (), and so on), Modcount plus 1

2, about the return value of the description

Returns False if the size of the collection A array does not change. If collection A and collection B are identical collections, false is also returned.

     Public Static void Main (string[] args) {        ArrayListnew arraylist<string>();        Lista.add ("Tom");        ArrayListNew arraylist<string>();        Listb.add ("Tom");        System.out.println (Lista.retainall (LISTB)); //false    }

Returns true even if the two sets have no intersection.

 Public Static void Main (string[] args) {        ArrayListnew arraylist<string>();        Lista.add ("Tom");        ArrayListNew arraylist<string>();        Listb.add ("Jack");        System.out.println (Lista.retainall (LISTB)); // true    }

So, let's remember that when the size of set a changes, the return is true, and the size does not change when the return is false.

Third, correct use of Retainall method
 Public Static voidMain (string[] args) {ArrayList<String> Lista=NewArraylist<string>(); Lista.add ("Tom"); ArrayList<String> listb=NewArraylist<string>(); Listb.add ("Tom");        Lista.retainall (LISTB); if(Lista.size () >0) {System.out.println ("The two collections have the same intersection"); }Else{System.out.println ("These two collections do not have the same intersection"); }    }

1, first call the method of Retainall

2. Determine if there is an intersection by judging the size of the set. cannot be judged by the true and False returned by the method.

Take you right to use the Retainall method of list to find the intersection

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.