Java Fetch intersection method Retainall

Source: Internet
Author: User

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).

    1. If the same element exists, only the same element is preserved in oldcourses.

    2. 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

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.