Java list Implementation Method efficiency (ArrayList, aggregate list, Vector, Stack), and java timestamp Acquisition Method Comparison, arraylistdetaillist

Source: Internet
Author: User

Java list Implementation Method efficiency (ArrayList, aggregate list, Vector, Stack), and java timestamp Acquisition Method Comparison, arraylistdetaillist
1. Introduction to list

List class, which stores any objects in sequence (the order remains unchanged) and can be repeated. List is an interface inherited from Collection and cannot be instantiated. For instantiation, ArrayList can be used to implement a dynamic array. The query speed is fast (random access or sequential access), and The deletion speed is slow. The whole task is quickly cleared and threads are not synchronized (non-thread safety ). The array length is a variable 50% extended list (linked list), slow query, fast addition and deletion. Vector (implementing dynamic arrays) is slow and replaced by ArrayList. Length can be extended. Thread Security (synchronous classes and functions are all synchronized) Stack (Implementation Stack) inherits from Vector, advanced and later. Therefore, quick access to ArrayList, quick addition, deletion, and single-thread access can be used. multithreading can only be used to insert: add () Search: get () Delete: remove (int index) with the basic synchronization-class Vectorlist operation) modify: set () clear table: clear () traversal: Use the Iterator to traverse each element, as shown in figure
List al = new ArrayList();Iterator it = al.iterator()while (it.hasNext()){    System.out.println(it.next().toString());}
Other functions: boolean add (int index, E element) boolean addAll (index, Collection)
2. Efficiency Comparison Between ArrayList and rule listPrinciple 2.1
Ordered storage stores data elements in a continuous bucket for sequential access or direct (by subscript) access. High storage efficiency and fast speed. However, once defined, the size of the space does not change during the entire operation of the program, so it is not easy to expand. At the same time, in order to keep the original order (there is no rule on the order of elements going into the stack) during insertion or deletion, the modification efficiency is not high because the average needs to move half (or nearly half) of elements. The linked storage indicates that the storage space is dynamically allocated and released during the running process of the program. As long as there is space in the memory, the storage overflow will not occur. At the same time, you do not need to maintain the original physical order of data elements during insertion and deletion. You only need to maintain the original logical order, so you do not need to move the data. You only need to modify their link pointers, high modification efficiency. However, when accessing data elements in a table, you can only access them in the chain order, so the access efficiency is not high.
2.2 code Test
package test;import java.util.ArrayList;import java.util.LinkedList;import java.util.List;public class ListTest {    public static void main(String[] args) {        List<String> LList = new LinkedList<String> ();        List<String> AList = new ArrayList<String> ();        long startTime = System.currentTimeMillis();        for(int i=0;i<10000;i++)        {            LList.add(""+i);        }        long endTime = System.currentTimeMillis();        long result = endTime - startTime;        System.out.println("linkedlist:"+result);        startTime = System.currentTimeMillis();        for(int i=0;i<10000;i++)        {            AList.add(""+i);        }        endTime = System.currentTimeMillis();        result = endTime - startTime;        System.out.println("arraylist:"+result);    }

The results show that the ArrayList speed is much faster than the sorted list speed.

Here, we use three methods to obtain the timestamp (in milliseconds) to calculate the time,
// Method 1: fastest
System. currentTimeMillis ();
// Method 2 is slower than method 1, which is twice the time
New Date (). getTime ();
// Method 3: The speed is the slowest, and the time consumed by Canlendar in processing the time zone
Calendar. getInstance (). getTimeInMillis ();

In addition, in the article http://blog.csdn.net/inkfish/article/details/5185320
For more information about list and performance comparison, see

Conclusion: 1. treeList is the most efficient in random insert and delete operations. 2. in an environment where only append and iteration are required, the efficiency of listing is the highest; 3. in terms of average efficiency, ArrayList is relatively balanced, but if a large number of random operations are performed, it will still cause performance bottlenecks; 4. copyOnWriteArrayList is used with caution because of thread security, which causes much lower performance. vector is less efficient than the legend. 6. let the Stack do the List thing, but the Stack should not do too many List tasks in semantics; 7. in sorting, ArrayList has the best performance, and the average performance of TreeList is also good. The sorting efficiency of sorted list is greatly affected by the initial state of elements. 8. There is almost no time loss for conversions between various lists.

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.