HashMap存入大量數據是否要預定義存儲空間


按說HashMap的負載極限為0.75,可是,測試程序並看不出這個結果。待探討

測試程序如下:

根據結果看不出來預定義有什么影響。

public class test {
    public static void main(String[] args) {
        testRun(5000000);
        testRun(10000000);
        testRun(15000000);
        testRun(20000000);
        testRun(25000000);
        testRun(30000000);
        testRun(35000000);
        testRun(40000000);
        testRun(45000000);
        testRun(50000000);
         
    }
    public static void testRun(int mapCount){
        System.out.println("============"+mapCount+"=============");
        run(mapCount,new HashMap<>());
        run(mapCount,new HashMap<>(mapCount));
        run(mapCount,new HashMap<>((int)(mapCount/0.75)));
        run(mapCount,new HashMap<>((int)(mapCount/0.6)));
        run(mapCount,new HashMap<>((int)(mapCount/0.5)));
        
    }
    
    public static void run(int mapCount,Map<Integer,String> testMap){
         String testValue = "";
         Long startTime0 = System.currentTimeMillis();
         for(int i = 1;i<mapCount;i++){
             testMap.put(i, testValue);
         }
         Long endTime0 = System.currentTimeMillis();
         System.out.println(endTime0-startTime0);
    }
}
============5000000=============
1670
689
981
260
211
============10000000=============
1097
4485
793
386
331
============15000000=============
768
3394
2805
5651
6022
============20000000=============
8155
2153
5089
6078
6809
============25000000=============
8890
6998
10050
9149
10501
============30000000=============
14467
10527
9334
11453
13733
============35000000=============
14493
11978
13706
13809
11461
============40000000=============
15788
13439
18142
13514
11351
============45000000=============
12952
18501
17793
11894
30664
============50000000=============
29528
14534
14272
32803
42236

 


免責聲明!

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



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