淺談Java中幾個常用集合添加元素的效率
初始化需要進行比較的集合,統(tǒng)一增加10萬個元素,獲取整個過程的執(zhí)行時間。
1、List集合增加元素
private static void testList() {
List<Integer> list = new ArrayList<Integer>();
long startTime = System.currentTimeMillis(); // 獲取開始時間
for (int i = 0; i < 100000; i++) {
list.add(i);
}
long endTime = System.currentTimeMillis(); // 獲取結束時間
System.out.println("List添加元素程序運行時間為:" + (endTime - startTime) + "ms"); // 輸出程序運行時間
}
程序輸出:
List添加10萬個元素程序運行時間為:8ms
2、Set集合增加元素
private static void testSet() {
Set<Integer> set = new HashSet<Integer>();
long startTime = System.currentTimeMillis(); // 獲取開始時間
for (int i = 0; i < 100000; i++) {
set.add(i);
}
long endTime = System.currentTimeMillis(); // 獲取結束時間
System.out.println("Set添加10萬個元素程序運行時間為:" + (endTime - startTime) + "ms"); // 輸出程序運行時間
}
程序輸出:
Set添加10萬個元素程序運行時間為:17ms
3、LinkedList集合增加元素
private static void testLinkedList() {
List<Integer> list = new LinkedList<Integer>();
long startTime = System.currentTimeMillis(); // 獲取開始時間
for (int i = 0; i < 100000; i++) {
list.add(i);
}
long endTime = System.currentTimeMillis(); // 獲取結束時間
// 輸出程序運行時間
System.out.println("LinkedList添加10萬個元素程序運行時間為:" + (endTime - startTime) + "ms");
}
程序輸出:
LinkedList添加10萬個元素程序運行時間為:8ms
4、TreeSet集合增加元素
private static void testTreeSet() {
Set<Integer> set = new TreeSet<Integer>();
long startTime = System.currentTimeMillis(); // 獲取開始時間
for (int i = 0; i < 100000; i++) {
set.add(i);
}
long endTime = System.currentTimeMillis(); // 獲取結束時間
// 輸出程序運行時間
System.out.println("TreeSet添加10萬個元素程序運行時間為:" + (endTime - startTime) + "ms");
}
程序輸出:
TreeSet添加10萬個元素程序運行時間為:40ms
總結:在不考慮去重和排序的情況下,以上幾個常用集合的執(zhí)行效率排序為:ArrayList >= LinkedList > HashSet > TreeSet
5、HashMap集合增加元素
private static void testHashMap() {
Map<Integer, Object> hashMap = new HashMap<Integer, Object>();
long startTime = System.currentTimeMillis(); // 獲取開始時間
for (int i = 0; i < 100000; i++) {
hashMap.put(i, "test");
}
long endTime = System.currentTimeMillis(); // 獲取結束時間
// 輸出程序運行時間
System.out.println("HashMap添加10萬個元素程序運行時間為:" + (endTime - startTime) + "ms");
}
程序輸出:
HashMap添加10萬個元素程序運行時間為:17ms
6、TreeMap集合增加元素
private static void testTreeMap() {
Map<Integer, Object> treeMap = new TreeMap<Integer, Object>();
long startTime = System.currentTimeMillis(); // 獲取開始時間
for (int i = 0; i < 100000; i++) {
treeMap.put(i, "test");
}
long endTime = System.currentTimeMillis(); // 獲取結束時間
// 輸出程序運行時間
System.out.println("TreeMap添加10萬個元素程序運行時間為:" + (endTime - startTime) + "ms");
}
程序輸出:
TreeMap添加10萬個元素程序運行時間為:40ms
總結:在不考慮排序的情況下,HashMap的執(zhí)行效率高于TreeMap:HashMap > TreeMap。
以上這篇淺談Java中幾個常用集合添加元素的效率就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
使用Java編寫一個簡單的Web的監(jiān)控系統(tǒng)
這篇文章主要介紹了使用Java編寫一個簡單的Web的監(jiān)控系統(tǒng)的例子,并且將重要信息轉為XML通過網(wǎng)頁前端顯示,非常之實用,需要的朋友可以參考下2015-11-11
Spring中的ApplicationRunner接口的使用詳解
這篇文章主要介紹了Spring中的ApplicationRunner接口的使用詳解,ApplicationRunner使用起來很簡單,只需要實現(xiàn)CommandLineRunner或者ApplicationRunner接口,重寫run方法就行,需要的朋友可以參考下2023-11-11
java.io.EOFException: Unexpected end of
本文主要介紹了java.io.EOFException: Unexpected end of ZLIB input stream異常解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-05-05
SpringBoot?快速實現(xiàn)?api?接口加解密功能
在項目中,為了保證數(shù)據(jù)的安全,我們常常會對傳遞的數(shù)據(jù)進行加密,Spring?Boot接口加密,可以對返回值、參數(shù)值通過注解的方式自動加解密,這篇文章主要介紹了SpringBoot?快速實現(xiàn)?api?接口加解密功能,感興趣的朋友一起看看吧2023-10-10

