Retainall methods in the Java collection

Source: Internet
Author: User

Import Java.util.arraylist;public class Retainalldemo {public static Boolean compare (Int[] arr1,int[] arr2) {ArrayList <Integer> list1=new arraylist<> (); Arraylist<integer> list2=new arraylist<> (); for (int a:arr1) {list1.add (a);} for (int b:arr2) {list2.add (b);} System.out.println (List1.retainall (List2)); return List1.retainall (List2);//list1 6, 5 The removal operation is not performed when the method is executed in List2 returns True if ARR1 is changed to {1,2,3,4}, the method does not need to be removed to return false;}public static void Main (string[] args) {int[] Arr1={1,2,3,4,6,5};int[] Arr2={1,2,3,4,53};boolean b=compare (ARR1,ARR2); System.out.println (".....:" +b);}}

Retainall (collection<?> c)
leave only those elements in this collection that are also contained in the specified collection (optional action). In other words, remove all elements in this collection that are not contained in the specified collection.

2, List.retainall (LIST2): 1) If the collection list=list2 is two elements of the collection exactly the same as the return value of False;2) list contained in the List2 return value of false;3) Other return value is true.

3, in fact, the method refers to: if the elements in the collection list in the collection List2, the elements in the list do not remove the operation, and vice versa if only one of the list2 is not in the removal operation. That is, the list is removed with the return value: True and the return value is false.

Retainall methods in the Java collection

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.