Java list de-weight mode and efficiency comparison

Source: Internet
Author: User
Tags addall

There are three ways to remove the list and ensure that the order is added:

Mode one, the use of hashset can not add duplicate data characteristics because HashSet can not guarantee the order of addition, so only as a criterion:

    Private Static void removeduplicate (list<string> List) {        HashSetnew hashset<string> (List.size ());        ListNew arraylist<string>(List.size ());          for (String str:list) {            if  (Set.add (str)) {                result.add (str);            }        }        List.clear ();        List.addall (result);    }

Mode two, using Linkedhashset cannot add duplicate data and can guarantee the addition order of the characteristics:

    Private Static void removeDuplicate2 (list<string> List) {        linkedhashsetnew linkedhashset< String>(List.size ());        Set.addall (list);        List.clear ();        List.addall (set);    }

Mode three, using the Contains method of the list to iterate through:

    Private Static void removeDuplicate3 (list<string> list) {        listnew arraylist<string> (List.size ());          for (String str:list) {            if (!  Result.contains (str)) {                result.add (str);            }        }        List.clear ();        List.addall (result);    }

Test method:

     Public Static voidMain (string[] args) {Finallist<string> list =NewArraylist<string>();  for(inti = 0; i < 1000; i++) {List.add ("Haha-" +i); }        LongTime =System.currenttimemillis ();  for(inti = 0; I < 10000; i++) {removeduplicate (list); }        LongTime1 =System.currenttimemillis (); System.out.println ("Time1:" + (time1-Time ));  for(inti = 0; I < 10000; i++) {removeDuplicate2 (list); }        LongTime2 =System.currenttimemillis (); System.out.println ("Time2:" + (time2-time1));  for(inti = 0; I < 10000; i++) {removeDuplicate3 (list); }        LongTime3 =System.currenttimemillis (); System.out.println ("Time3:" + (time3-time2)); }

Test results:

time1:451
time2:579
time3:62113

Conclusion:

It is recommended to use method one or method two way to carry out the weight

Java list de-weight mode and efficiency comparison

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.