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

Source: Internet
Author: User

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

The set to be compared during initialization. A total of 0.1 million elements are added to obtain the execution time of the entire process.

1. add elements to the List set

1 private static void testList () {2 3 List <Integer> list = new ArrayList <Integer> (); 4 5 long startTime = System. currentTimeMillis (); // get start time 6 for (int I = 0; I <100000; I ++) {7 8 list. add (I); 9 10} 11 long endTime = System. currentTimeMillis (); // get end time 12 13 System. out. println ("the running time of the List add element is:" + (endTime-startTime) + "ms"); // The output running time is 14 15}

Program output:

The running time of adding 0.1 million elements to the List is 8 ms.

2. add elements to the Set

1 private static void testSet () {2 3 Set <Integer> set = new HashSet <Integer> (); 4 5 long startTime = System. currentTimeMillis (); // get start time 6 for (int I = 0; I <100000; I ++) {7 8 set. add (I); 9 10} 11 long endTime = System. currentTimeMillis (); // get end time 12 13 System. out. println ("Set to add 0.1 million elements for running time:" + (endTime-startTime) + "ms"); // output for running time 14 15}

Program output:

Run Time of 0.1 million elements added to Set: 17 ms

3. add elements to the sorted list set

1 private static void testparts List () {2 3 list <Integer> List = new parts list <Integer> (); 4 5 long startTime = System. currentTimeMillis (); // get start time 6 for (int I = 0; I <100000; I ++) {7 8 list. add (I); 9 10} 11 long endTime = System. currentTimeMillis (); // get the end time 12 13 // output the program running time 14 System. out. println ("add 0.1 million elements to the shortlist program run time:" + (endTime-startTime) + "ms"); 15 16}

Program output:

The program running time of adding 0.1 million elements to the shortlist is 8 ms.

4. add elements to the TreeSet set

1 private static void testTreeSet () {2 3 Set <Integer> set = new TreeSet <Integer> (); 4 5 long startTime = System. currentTimeMillis (); // get start time 6 for (int I = 0; I <100000; I ++) {7 8 set. add (I); 9 10} 11 long endTime = System. currentTimeMillis (); // get the end time 12 13 // output the program running time 14 System. out. println ("add 0.1 million elements to the TreeSet:" + (endTime-startTime) + "ms"); 15 16}

Program output:

The running time of 0.1 million elements added to the TreeSet is 40 ms.

Conclusion: without removing and sorting, the efficiency sorting of the preceding common sets is as follows: ArrayList> = sort list> HashSet> TreeSet.

5. add elements to the HashMap set

1 private static void testHashMap () {2 3 Map <Integer, Object> hashMap = new HashMap <Integer, Object> (); 4 5 long startTime = System. currentTimeMillis (); // get start time 6 for (int I = 0; I <100000; I ++) {7 hashMap. put (I, "test"); 8} 9 10 long endTime = System. currentTimeMillis (); // get the end time 11 12 // output the program running time 13 System. out. println ("the running time of adding 0.1 million elements to HashMap is:" + (endTime-startTime) + "ms"); 14 15}

Program output:

Run Time of 0.1 million elements added to HashMap: 17 ms

6. add elements to the TreeMap set

1 private static void testTreeMap () {2 3 Map <Integer, Object> treeMap = new TreeMap <Integer, Object> (); 4 5 long startTime = System. currentTimeMillis (); // get start time 6 for (int I = 0; I <100000; I ++) {7 treeMap. put (I, "test"); 8} 9 10 long endTime = System. currentTimeMillis (); // get the end time 11 12 // output the program running time 13 System. out. println ("the running time of adding 0.1 million elements to TreeMap is:" + (endTime-startTime) + "ms"); 14}

Program output:

The running time of 0.1 million elements added to TreeMap is 40 ms.

Conclusion: without sorting, the execution efficiency of HashMap is higher than that of TreeMap: HashMap> TreeMap.

 

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.