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