日期:2014-05-20  浏览次数:20698 次

关于集合类查找速度
HashMap,Array,List
这三个速度比较是怎样的?

------解决方案--------------------
看来是我错了Array比ArrayList快,我用了个比较简单的例子测试了下Array比ArrayList快了很多~
Java code

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class TimeTest {

    public static void main(String[] args) {
        int[] a = new int[999999];
        ArrayList<Integer> b = new ArrayList<Integer>();
        for(int i = 0 ; i < 999999;i++){
            a[i] = i;
            b.add(i);
        }
        
        Calendar c = new GregorianCalendar();
        long time1 = c.getTimeInMillis();
        for(int i = 0 ; i< 999999 ; i++){
            if(a[i] == 999998){
                break;
            }
        }
        c = new GregorianCalendar();
        long time2 = c.getTimeInMillis();
        System.out.println("数组查找到999998,所消耗的时间:" + (time2 - time1));
        c = new GregorianCalendar();
        time1 = c.getTimeInMillis();
        for(Integer num : b){
            if(num == 999998){
                break;
            }
        }
        c = new GregorianCalendar();
        time2 = c.getTimeInMillis();
        System.out.println("ArrayList查找到999998,所消耗的时间:" + (time2 - time1));
        
        
    }

}