Java Fetch intersection method Retainall
There are two sets of newcoures and oldcourses that determine whether the two collections contain the same object or element.
You can use the Retainall method: Oldcourses.retainall (Newcoures).
-
If the same element exists, only the same element is preserved in oldcourses.
-
If the same element does not exist, the oldcourse becomes empty.
If there are multiple collections oldCourses1, OldCourses2, OldCourses3, and so on, respectively, compared with newcourses, you should put newcourses unity behind, like this:
Oldcourses1.retainall (Newcoures);
Oldcourses2.retainall (Newcoures);
Oldcourses3.retainall (Newcoures);
import java.util.arraylist;import java.util.list;/** * Test Retainall (); * created by lgq on 2015/8/11. */public class test { public static void main (String[] args) { List<String> list1 = new ArrayList<String> (); List<String> list2 = new ArrayList<String> (); list<string> list3 = new arraylist <String> (; for ) (int i = 0; i < 20; i++) { List1.add (i+ ""); if (i%2 == 0) { list2.add (i+ ""); } list3.add (i+ "@"); } // list1 has the same element as list2 , the List1 collection retains only the elements that exist in List2 list1.retainall (List2); if ( List1.isempty ()) { System.out.println ("not included"); } else { system.out.println ("contains"); } system.out.println (List1); // list1 and list3 do not have the same element, the List1 collection becomes empty list1.retainall ( LIST3); if (List1.isempty ()) { system.out.println ("not included"); } else { System.out.println ("contains"); } system.out.println (list1); }}
Java Fetch intersection method Retainall