Get the different elements of the two list

Source: Internet
Author: User

Get the different elements of the two list

/**  *  several different methods, different query efficiency  *getdiffrent5 () highest efficiency  */  import  java.util.arraylist;  import java.util.hashmap;  import java.util.list;   import java.util.Map;    /**  *  gets the different objects in the two list collection   */   public class test2listdiff {        public  static void main (String[] args)  {           List<String> list1 = new ArrayList<String> ();           List<String> list2 = new ArrayList< String> ();          int num = 5;           // int num=5000;            for (int i = 0; i < num; i++)  {               list1.add ("Test_"  + i);               list2.add ("Test_"  + i * 2);           }             getdiffrent3 (LIST1,&NBSP;LIST2);          &NBSP;GETDIFFRENT5 (LIST1,&NBSP;LIST2);           getdiffrent4 (LIST1,&NBSP;LIST2);           getdiffrent2 (List1, list2 );           getdiffrent (LIST1,&NBSP;LIST2);             // getDiffrent3 total times  32271699           // getdiffrent5 total times 12239545            // getDiffrent4 total times  16786491           // getdiffrent2 total  times 2438731459       }         /**      *  get different elements of the two list       *        *  @param  list1      * @ param list2      *  @return       */  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PRIVATE&NBSP;STATIC&NBSP;LIST&LT;STRING&GT;&NBSP;GETDIFFRENT5 (List<String> &NBSP;LIST1,&NBSP;LIST&LT;STRING&GT;&NBSP;LIST2)  {           long st =&nbSp System.nanotime ();           list<string> diff  = new ArrayList<String> ();           list<string> maxlist = list1;           list<string> minlist = list2;           if  (List2.size ()  > list1.size ())  {               maxList = list2;               minList = list1;           }            //  save data from list to map            map<string, integer> maxmap  = new  Hashmap<string, integer> (Maxlist.size ());           for  (string string : maxlist)  {               maxmap.put (string, 1);           }            // max :1,2,3,4,5           // min:2,4,6,8,10              //  loop values in Minlist, marking in  maxmap   The same   data 2           for  (String string  : minlist)  {               //  the same                if   (Maxmap.get (String)  !=&nbsP;null)  {                   maxmap.put (string, 2);                   continue;               }               //  Unequal                 Diff.add (String);          }           printf (diff);             //  Circulating maxmap           for  (Map.Entry <string, integer> entry : maxmap.entryset ())  {               if  (Entry.getvalue ()  == 1)  {                   diff.add (Entry.getKey ());               }           }          printf (diff);             system.out.println ("getDiffrent5 total  times  " +  (System.nanotime ()  - st));           return diff;      }         /**      *  get two different elements of the list       *        *  @param  list1      *  @param  list2      *  @return       */       private static list<string> getdiffrent4 (list<string> list1, list &LT;STRING&GT;&NBSP;LIST2)  {          long st  = system.nanotime ();           map<string,  Integer> map = new HashMap<String, Integer> (List1.size ()  +  List2.size ());           list<string> diff =  new ArrayList<String> ();          list< string> maxlist = list1;          list< string> minlist = list2;          if  (List2.size ()  > List1.size ())  {               maxlist = list2;               minlist = list1;          }           for  (string string : maxlist)  {               map.put (string, 1);           }           for  (string string : minlist)  {               integer cc = map.get (String);               if  (cc != null)  {         &Nbsp;         map.put (STRING,&NBSP;++CC);                   continue;               }               map.put (string, 1);           }          for  (map.entry< String, integer> entry : map.entryset ())  {               if  (Entry.getvalue ()  == 1)  {                   diff.add ( Entry.getkey ());              }       &nbsP;   }          system.out.println (" getdiffrent4 total times  " +  (System.nanotime ()  - st));           return diff;      }         /**      *  get two different elements of the list        *       *  @param  list1       *  @param  list2      *  @return        */      private static List<String>  GetDiffrent3 (LIST&LT;STRING&GT;&NBSP;LIST1,&NBSP;LIST&LT;STRING&GT;&NBSP;LIST2)  {           long st = system.nanotime ();           map<String, Integer> map = new HashMap<String, Integer> (List1.size ()  + list2.size ());          list<string>  diff = new ArrayList<String> ();           for  (String string : list1)  {               map.put (string, 1);           }          for  (string string &NBSP;:&NBSP;LIST2)  {               Integer cc = map.get (String);               if  (cc != null)  {                   map.put (STRING,&NBSP;++CC);                   continue;               }               map.put (string, 1);          }           for  (map.entry<string, integer>  Entry : map.entryset ())  {               if  (Entry.getvalue ()  == 1)  {                   diff.add (Entry.getkey ());               }           }  &nBsp;       system.out.println ("getdiffrent3 total times "  +  (System.nanotime ()  - st));           return diff;      }        /**       *  get different elements of a list       *        *  @param  list1      *  @param   list2      *  @return       */       private static list<string> getdiffrent2 (List<String>  LIST1,&NBSP;LIST&LT;STRING&GT;&NBSP;LIST2)  {           Long st = system.nanotime ();           List1.retainall (LIST2);   &nbSp;       system.out.println ("getdiffrent2 total times "  +  (System.nanotime ()  - st));           return list1;      }        /**       *  get two different elements of the list       *        *  @param  list1      *  @param   list2      *  @return       */       private static list<string> getdiffrent (List<String>  LIST1,&NBSP;LIST&LT;STRING&GT;&NBSP;LIST2)  {           Long st = system.nanotime ();           list <string> diff =&nbsP;new arraylist<string> ();          for  ( STRING&NBSP;STR&NBSP;:&NBSP;LIST1)  {               if  (!list2.contains (str))  {                   diff.add (str);               }          }           system.out.println ("Getdiffrent total times    +  (System.nanotime ()  - st);           return diff;      }         public static void printf (list<string> list)  {          &nbsp System.out.println ("----------------------------");           for  (Int i = 0; i < list.size ();  i++)  {               system.out.println (List.get (i));           }      }

Get different elements of the two list

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.