比較Java中幾個常用集合添加元素的效率


初始化需要進行比較的集合,統一增加10萬個元素,獲取整個過程的執行時間。

1、List集合增加元素

 1 private static void testList() {
 2 
 3         List<Integer> list = new ArrayList<Integer>();
 4 
 5         long startTime = System.currentTimeMillis(); // 獲取開始時間
 6         for (int i = 0; i < 100000; i++) {
 7 
 8             list.add(i);
 9 
10         }
11         long endTime = System.currentTimeMillis(); // 獲取結束時間
12 
13         System.out.println("List添加元素程序運行時間為:" + (endTime - startTime) + "ms"); // 輸出程序運行時間
14 
15     }

程序輸出:

List添加10萬個元素程序運行時間為:8ms

2、Set集合增加元素

 1 private static void testSet() {
 2 
 3         Set<Integer> set = new HashSet<Integer>();
 4 
 5         long startTime = System.currentTimeMillis(); // 獲取開始時間
 6         for (int i = 0; i < 100000; i++) {
 7 
 8             set.add(i);
 9 
10         }
11         long endTime = System.currentTimeMillis(); // 獲取結束時間
12 
13         System.out.println("Set添加10萬個元素程序運行時間為:" + (endTime - startTime) + "ms"); // 輸出程序運行時間
14 
15     }

程序輸出:

Set添加10萬個元素程序運行時間為:17ms

3、LinkedList集合增加元素

 1 private static void testLinkedList() {
 2 
 3         List<Integer> list = new LinkedList<Integer>();
 4 
 5         long startTime = System.currentTimeMillis(); // 獲取開始時間
 6         for (int i = 0; i < 100000; i++) {
 7 
 8             list.add(i);
 9 
10         }
11         long endTime = System.currentTimeMillis(); // 獲取結束時間
12 
13         // 輸出程序運行時間
14         System.out.println("LinkedList添加10萬個元素程序運行時間為:" + (endTime - startTime) + "ms");
15 
16     }

程序輸出:

LinkedList添加10萬個元素程序運行時間為:8ms

4、TreeSet集合增加元素

 1 private static void testTreeSet() {
 2 
 3         Set<Integer> set = new TreeSet<Integer>();
 4 
 5         long startTime = System.currentTimeMillis(); // 獲取開始時間
 6         for (int i = 0; i < 100000; i++) {
 7 
 8             set.add(i);
 9 
10         }
11         long endTime = System.currentTimeMillis(); // 獲取結束時間
12 
13         // 輸出程序運行時間
14         System.out.println("TreeSet添加10萬個元素程序運行時間為:" + (endTime - startTime) + "ms");
15 
16     }

程序輸出:

TreeSet添加10萬個元素程序運行時間為:40ms

總結:在不考慮去重和排序的情況下,以上幾個常用集合的執行效率排序為:ArrayList >= LinkedList > HashSet > TreeSet

5、HashMap集合增加元素

 1 private static void testHashMap() {
 2 
 3         Map<Integer, Object> hashMap = new HashMap<Integer, Object>();
 4 
 5         long startTime = System.currentTimeMillis(); // 獲取開始時間
 6         for (int i = 0; i < 100000; i++) {
 7             hashMap.put(i, "test");
 8         }
 9 
10         long endTime = System.currentTimeMillis(); // 獲取結束時間
11 
12         // 輸出程序運行時間
13         System.out.println("HashMap添加10萬個元素程序運行時間為:" + (endTime - startTime) + "ms");
14 
15     }

程序輸出:

HashMap添加10萬個元素程序運行時間為:17ms

6、TreeMap集合增加元素

 1 private static void testTreeMap() {
 2         
 3         Map<Integer, Object> treeMap = new TreeMap<Integer, Object>();
 4 
 5         long startTime = System.currentTimeMillis(); // 獲取開始時間
 6         for (int i = 0; i < 100000; i++) {
 7             treeMap.put(i, "test");
 8         }
 9 
10         long endTime = System.currentTimeMillis(); // 獲取結束時間
11 
12         // 輸出程序運行時間
13         System.out.println("TreeMap添加10萬個元素程序運行時間為:" + (endTime - startTime) + "ms");
14     }

程序輸出:

TreeMap添加10萬個元素程序運行時間為:40ms

總結:在不考慮排序的情況下,HashMap的執行效率高於TreeMap:HashMap > TreeMap。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM