import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestCollection {
private List int_list;
private Map int_map;
public void testListSpeed(Integer[] search_int){
for(Integer n : search_int){
for(int i=0;i
if(n==int_list.get(i))
break;
}
}
}
public void testMapSpeed(Integer[] search_int){
for(Integer n : search_int){
if(int_map.containsKey(n))
int_map.get(n);
}
}
public void init(Integer[] search_int){
int_list = new ArrayList();
int_map = new HashMap();
for(int i=0;i
int_list.add(search_int[i]);
int_map.put(search_int[i], search_int[i]);
}
}
public static void main(String[] args){
int max = 10000;
Integer[] x = new Integer[max];
for(int j=0;j
x[j] = j;
}
TestCollection tc = new TestCollection();
long start = System.currentTimeMillis();
tc.init(x);
long finish = System.currentTimeMillis();
System.out.println("初始化"+max+"个数据用时间 = "+(finish-start)+" ms");
start = System.currentTimeMillis();
tc.testListSpeed(x);
finish = System.currentTimeMillis();
System.out.println("执行testListSpeed用时 = "+(finish-start)+" ms");
start = System.currentTimeMillis();
tc.testMapSpeed(x);
finish = System.currentTimeMillis();
System.out.println("执行testMapSpeed用时 = "+(finish-start)+" ms");
}
}
结果
初始化10000个数据用时间 = 10 ms
执行testListSpeed用时 = 720 ms
执行testMapSpeed用时 = 0 ms
import java.util.Map;
public class TestCollection {
}
结果
初始化10000个数据用时间 = 10 ms
执行testListSpeed用时 = 720 ms
执行testMapSpeed用时 = 0 ms