Java集合类之List性能比较

ArrayList/LinkedList/Vector 三者添加元素、删除元素、遍历耗时对比

package com.loongshaw;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

/** * * @author loongshaw * @note 比较ArrayList LinkedList Vector三中类型的列表操作效率 * */
public class ListComparation {

    public static void arrayListTest(int counts){

        long startTime = System.currentTimeMillis();

        List list = new ArrayList();
        Integer obj = Integer.valueOf(1);

        for(int i=0;i<counts;i++){

            list.add(obj);
        }       
        long addTime = System.currentTimeMillis();

        for(int k=0;k<counts;k++){

            list.get(k);
        }   

        /** Iterator iter = list.iterator(); while(iter.hasNext()){ iter.next(); } **/

        long getTime = System.currentTimeMillis();

        for(int j=0;j<counts;j++){

            list.remove(obj);
        }   

        long deleteTime = System.currentTimeMillis();

        System.out.println("ArrayList 添加"+counts+"个对象耗时"+(addTime-startTime)+",遍历耗时:"+(getTime-addTime)+",删除耗时:"+(deleteTime-getTime));
    }

    public static void linkedListTest(int counts){

        long startTime = System.currentTimeMillis();

        LinkedList list = new LinkedList();

        Integer obj = Integer.valueOf(1);

        for(int i=0;i<counts;i++){

            list.addLast(obj);
        }       
        long addTime = System.currentTimeMillis();

        for(int k=0;k<counts;k++){

            list.getFirst();
        }   

        /** Iterator iter = list.iterator(); while(iter.hasNext()){ iter.next(); } **/

        long getTime = System.currentTimeMillis();

        for(int j=0;j<counts;j++){

            list.remove(obj);
        }   

        long deleteTime = System.currentTimeMillis();

        System.out.println("LinkedList 添加"+counts+"个对象耗时"+(addTime-startTime)+",遍历耗时:"+(getTime-addTime)+",删除耗时:"+(deleteTime-getTime));
    }

    public static void vectorTest(int counts){

        long startTime = System.currentTimeMillis();

        List list = new Vector();

        Integer obj = Integer.valueOf(1);

        for(int i=0;i<counts;i++){

            list.add(obj);
        }       
        long addTime = System.currentTimeMillis();

        for(int k=0;k<counts;k++){

            list.get(k);
        }   

        /** Iterator iter = list.iterator(); while(iter.hasNext()){ iter.next(); } **/

        long getTime = System.currentTimeMillis();

        for(int j=0;j<counts;j++){

            list.remove(obj);
        }   

        long deleteTime = System.currentTimeMillis();

        System.out.println("Vector 添加"+counts+"个对象耗时"+(addTime-startTime)+",遍历耗时:"+(getTime-addTime)+",删除耗时:"+(deleteTime-getTime));        
    }

    public static void main(String[] args){

        int counts = 10000;

        arrayListTest(counts);

        linkedListTest(counts);

        vectorTest(counts);
    }
}

操作1万个数据

时间单位:毫秒

ArrayList 添加10000个对象耗时1,遍历耗时:0,删除耗时:8
LinkedList 添加10000个对象耗时1,遍历耗时:1,删除耗时:1
Vector 添加10000个对象耗时1,遍历耗时:2,删除耗时:8

操作10万个数据

时间单位:毫秒

ArrayList 添加100000个对象耗时4,遍历耗时:2,删除耗时:843
LinkedList 添加100000个对象耗时4,遍历耗时:2,删除耗时:4
Vector 添加100000个对象耗时5,遍历耗时:5,删除耗时:823

操作100万个数据

时间单位:毫秒

ArrayList 添加1000000个对象耗时17,遍历耗时:4,删除耗时:125005
LinkedList 添加1000000个对象耗时69,遍历耗时:3,删除耗时:9
Vector 添加1000000个对象耗时10,遍历耗时:8,删除耗时:119343

对比数据

操作1万个数据

集合类 遍历
ArrayList 1 8 0
LinkedList 1 1 1
Vector 1 8 2

操作10万个数据

集合类 遍历
ArrayList 4 843 2
LinkedList 4 4 2
Vector 5 823 5

操作100万个数据

集合类 遍历
ArrayList 17 125005 4
LinkedList 69 9 3
Vector 10 119343 8

测试环境

jdk8+eclipse+macpro

结论

根据耗时表格, 我们可以判断何时该用ArrayList,何时该用LinkedList.

总的来说, LinkedList更适用于:

  • 大量的增加/删除操作

ArrayList和Vector两者操作性能差异不大,但ArrayList适合开发者自由控制其同步状态。

你可能感兴趣的:(java,vector,ArrayList,LinkedList)