The efficiency of several implementations of the Java list (ArrayList, LinkedList, Vector, Stack), and three ways to obtain Java timestamp comparison

Source: Internet
Author: User

First, List introduction
List列表类,顺序存储任何对象(顺序不变),可重复。List是继承于Collection的接口,不能实例化。实例化可以用:    ArrayList(实现动态数组),查询快(随意访问或顺序访问),增删慢。整体清空快,线程不同步(非线程安全)。数组长度是可变的百分之五十延长    LinkedList(实现链表),查询慢,增删快。    Vector(实现动态数组),都慢,被ArrayList替代。长度任意延长。线程安全(同步的类,函数都是synchronized)    Stack(实现堆栈)继承于Vector,先进后出。所以,快速访问ArrayList,快速增删LinkedList,单线程都可以用,多线程只能用同步类Vectorlist基本操作    插入:add()    查找:get()    删除:remove(int index)    修改:set()    清空表:clear()    遍历:用Iterator迭代器遍历每个元素,如
new ArrayList();Iterator it = al.iterator()while (it.hasNext()){    System.out.println(it.next().toString());}
此外,其他函数    boolean add(int index, E element)    boolean addAll(index,Collection)
comparison of efficiency between ArrayList and LinkedList2.1 Principle
    顺序存储是将数据元素存放于一个连续的存储空间中,实现顺序存取或(按下标)直接存取。存储效率高,速度快。但空间大小一经定义,在程序整个运行期间不会发生改变,因此,不易扩充。同时,由于在插入或删除时,为保持原有次序(没有规定元素进栈顺序),平均需要移动一半(或近一半)元素,修改效率不高。    链接存储表示的存储空间一般在程序的运行过程中动态分配和释放,且只要存储器中还有空间,就不会产生存储溢出的问题。同时在插入和删除时不需要保持数据元素原来的物理顺序,只需要保持原来的逻辑顺序,因此不必移动数据,只需修改它们的链接指针,修改效率较高。但存取表中的数据元素时,只能循链顺序访问,因此存取效率不高。
2.2 Code Testing
 PackageTestImportJava.util.ArrayList;ImportJava.util.LinkedList;ImportJava.util.List; Public  class listtest {     Public Static void Main(string[] args) {List<string> llist =NewLinkedlist<string> (); List<string> alist =NewArraylist<string> ();LongStartTime = System.currenttimemillis (); for(intI=0;i<10000; i++) {Llist.add (""+i); }LongEndTime = System.currenttimemillis ();Longresult = Endtime-starttime; System.out.println ("LinkedList:"+result); StartTime = System.currenttimemillis (); for(intI=0;i<10000; i++) {Alist.add (""+i);        } endTime = System.currenttimemillis ();        result = Endtime-starttime; System.out.println ("ArrayList:"+result); }

It turns out that ArrayList faster than LinkedList.

Here are three ways to get a timestamp (in milliseconds) to calculate the time
Method one speed fastest
System.currenttimemillis ();
Method Two is slower than the method, less than one times
New Date (). GetTime ();
Method three speed slowest, Canlendar processing time zone time
Calendar.getinstance (). Gettimeinmillis ();

In addition, the article http://blog.csdn.net/inkfish/article/details/5185320
Refer to more list, and performance comparison, can see

结论:   1.随机插入、随机删除操作中,用TreeList 效率最高;  2.在只需要追加、迭代的环境下,LinkedList 效率最高;  3.平均效率来讲,ArrayList 相对平衡,但如果海量随机操作,还是会造成性能瓶颈;  4.CopyOnWriteArrayList 因为线程安全的原因,致使性能降低很多,所以慎用;  5.Vector 没有传说中那么低的效率;  6.让Stack 来做List 的事可以,不过语义上Stack 不应该做过多的List 的事情;  7.在排序中,ArrayList 具有最好的性能,TreeList 平均性能也不错,LinkedList 的排序效率受元素初始状态的影响很大。  8.各种List 间转换几乎没有时间损耗。

The efficiency of several implementations of the Java list (ArrayList, LinkedList, Vector, Stack), and three ways to obtain Java timestamp comparison

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.