Compare the efficiency of adding elements to several common collections in Java

Source: Internet
Author: User

Initializes a collection that needs to be compared, adding 100,000 elements uniformly to get the execution time of the entire process.

1. Add elements to list collection

1 Private Static voidtestlist () {2 3list<integer> list =NewArraylist<integer>();4 5         LongStartTime = System.currenttimemillis ();//Get start time6          for(inti = 0; I < 100000; i++) {7 8 List.add (i);9 Ten         } One         LongEndTime = System.currenttimemillis ();//Get end Time A  -SYSTEM.OUT.PRINTLN ("List add element program run time:" + (Endtime-starttime) + "MS");//Output program run time -  the}

Program output:

List add 100,000 elements program run time is: 8ms

2. Add elements to set set

1 Private Static voidTestset () {2 3Set<integer> set =NewHashset<integer>();4 5         LongStartTime = System.currenttimemillis ();//Get start time6          for(inti = 0; I < 100000; i++) {7 8 Set.add (i);9 Ten         } One         LongEndTime = System.currenttimemillis ();//Get end Time A  -System.out.println ("Set add 100,000 Element program run time:" + (Endtime-starttime) + "MS");//Output program run time -  the}

Program output:

Set add 100,000 elements program run time is: 17ms

3, LinkedList set add element

1 Private Static voidtestlinkedlist () {2 3list<integer> list =NewLinkedlist<integer>();4 5         LongStartTime = System.currenttimemillis ();//Get start time6          for(inti = 0; I < 100000; i++) {7 8 List.add (i);9 Ten         } One         LongEndTime = System.currenttimemillis ();//Get end Time A  -         //Output program run time -System.out.println ("LinkedList add 100,000 Element program run time:" + (Endtime-starttime) + "MS"); the  -}

Program output:

LinkedList add 100,000 Elements program run time: 8ms

4, TreeSet set add element

1 Private Static voidTesttreeset () {2 3Set<integer> set =NewTreeset<integer>();4 5         LongStartTime = System.currenttimemillis ();//Get start time6          for(inti = 0; I < 100000; i++) {7 8 Set.add (i);9 Ten         } One         LongEndTime = System.currenttimemillis ();//Get end Time A  -         //Output program run time -System.out.println ("TreeSet add 100,000 Element program run time:" + (Endtime-starttime) + "MS"); the  -}

Program output:

TreeSet Add 100,000 Elements program run time: 40ms

Summary: The execution efficiency of the above-mentioned collections is sorted as follows: ArrayList >= linkedlist > HashSet > TreeSet, regardless of the weight and sequencing

5, HashMap set add element

1 Private Static voidTesthashmap () {2 3Map<integer, object> HashMap =NewHashmap<integer, object>();4 5         LongStartTime = System.currenttimemillis ();//Get start time6          for(inti = 0; I < 100000; i++) {7Hashmap.put (i, "test"));8         }9 Ten         LongEndTime = System.currenttimemillis ();//Get end Time One  A         //Output program run time -System.out.println ("HashMap add 100,000 Element program run time:" + (Endtime-starttime) + "MS"); -  the}

Program output:

HashMap Add 100,000 Elements program run time: 17ms

6, TreeMap Set add element

1 Private Static voidTesttreemap () {2         3Map<integer, object> TreeMap =NewTreemap<integer, object>();4 5         LongStartTime = System.currenttimemillis ();//Get start time6          for(inti = 0; I < 100000; i++) {7Treemap.put (i, "test"));8         }9 Ten         LongEndTime = System.currenttimemillis ();//Get end Time One  A         //Output program run time -System.out.println ("TreeMap add 100,000 Element program run time:" + (Endtime-starttime) + "MS"); -}

Program output:

TreeMap Add 100,000 Elements program run time: 40ms

Summary: The execution efficiency of HASHMAP is higher than treemap:hashmap > TreeMap without regard to sequencing.

Compare the efficiency of adding elements to several common collections in Java

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.