Java的运行速度与效率一直令人诟病,为了提高运行效率,选择合适的类和方法,是我们开发人员的重点,同样的结果,不同的方法会有很大的运行效率区别,这里就分析了JAVA中最常用的三种集合,以及其运行速度,供大家参考:
package cn.java.test.xls;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
//测试ArrayList,set,hashMap的速度
public class list_test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Spend time arraylist add 1000000 value:"+runing_time_array()+"毫秒");
System.out.println("Spend time linklist add 1000000 value:"+(runing_time_link())+"毫秒");
System.out.println("Spend time HashMap add 1000000 value:"+(runing_time_hashmap())+"毫秒");
System.out.println("Spend time arraylist remove 10000 value:"+runing_time_array_remove()+"毫秒");
System.out.println("Spend time linklist remove 10000 value:"+(runing_time_link_remove())+"毫秒");
System.out.println("Spend time HashMap remove 10000 value:"+(runing_time_hashmap_remove())+"毫秒");
}
public static double runing_time_array(){
double startTime;
double stopTime;
startTime=new Date().getTime();
ArrayList al=new ArrayList();
for (int i=0;i<1000000;i++){
al.add("No"+i);
}
stopTime=new Date().getTime();
return (stopTime-startTime);
}
public static double runing_time_link(){
double startTime;
double stopTime;
startTime=new Date().getTime();
LinkedList ll=new LinkedList();
for (int i=0;i<1000000;i++){
ll.add("No"+i);
}
stopTime=new Date().getTime();
return (stopTime-startTime);
}
public static double runing_time_hashmap()
{
double startTime;
double stopTime;
startTime=new Date().getTime();
HashMap HM=new HashMap();
for (int i=0;i<1000000;i++){
HM.put(i, "No"+i);
}
stopTime=new Date().getTime();
return (stopTime-startTime);
}
public static double runing_time_array_remove(){
double startTime;
double stopTime;
ArrayList al=new ArrayList();
for (int i=0;i<1000000;i++){
al.add("No"+i);
}
startTime=new Date().getTime();
for (int i=0;i<10000;i++)
{
// al.remove(i);
al.remove("No"+i);
}
stopTime=new Date().getTime();
System.out.println(al.size());
return (stopTime-startTime);
}
public static double runing_time_link_remove(){
double startTime;
double stopTime;
LinkedList ll=new LinkedList();
for (int i=0;i<1000000;i++){
ll.add("No"+i);
}
startTime=new Date().getTime();
for (int i=0;i<10000;i++)
{
// ll.remove(i);
ll.remove("No"+i);
}
stopTime=new Date().getTime();
System.out.println(ll.size());
return (stopTime-startTime);
}
public static double runing_time_hashmap_remove()
{
double startTime;
double stopTime;
HashMap HM=new HashMap();
for (int i=0;i<1000000;i++){
HM.put(i, "No"+i);
}
startTime=new Date().getTime();
for (int i=0;i<10000;i++)
{
HM.remove(i);
}
stopTime=new Date().getTime();
System.out.println(HM.size());
return (stopTime-startTime);
}
}
事实证明,在添加元素方面,三个类的运行效率没太大的差别,但在删除元素方面,差别就比较大了,证明HashMap的删除元素的速度是最快的,其次是LinkList,最慢的是ArrayList,其速度不是一般的慢,如果不注意还真的被坑了。
验证结果:
Spend time arraylist add 1000000 value:437.0毫秒
Spend time linklist add 1000000 value:379.0毫秒
Spend time HashMap add 1000000 value:645.0毫秒
990000
Spend time arraylist remove 10000 value:5730.0毫秒
990000
Spend time linklist remove 10000 value:75.0毫秒
990000
Spend time HashMap remove 10000 value:1.0毫秒