Differences between the arraylist and arraylist

Source: Internet
Author: User

We generally know the differences between arraylist and rule list:
1. arraylist is a dynamic array-based data structure. The arraylist is based on the linked list data structure.

2. for Random Access to get and set, arraylist thinks it is better than the sorted list because the sorted list needs to move the pointer.

3. For add and remove operations, linedlist is dominant because arraylist needs to move data.

Arraylist and referlist are two collection classes used to store a series of object references ). For example, we can use arraylist to store a series of strings or integers. So what is the performance difference between arraylist and javaslist? When should I use arraylist? When should I use the rule list?

I. Time Complexity
First, the key point is that the internal implementation of arraylist is based on the basic object array. Therefore, when it uses the get method to access any element in the list (random access ), it is faster than the upload list. The get method in the detail list starts to check from one end of the list in sequence until the other end. For the shortlist, there is no faster way to access a specified Element in the list.

Suppose we have a large list, and the elements in it are sorted. This list may be of the arraylist type or the sorted list type, now we can perform binary search on this list. Compare the query speed when the list is arraylist and sorted list. See the following program:

Java code
  1. Package com. Mangocity. test;
  2. Import java. util. Collections list;
  3. Import java. util. List;
  4. Import java. util. Random;
  5. Import java. util. arraylist;
  6. Import java. util. arrays;
  7. Import java. util. collections;
  8. Public class testlist ...{
  9. Public static final int n= 50000;
  10. Public static list values;
  11. Static ...{
  12. Integer Vals [] = new integer [N];
  13. Random r = new random ();
  14. For (INT I = 0, currval = 0; I <n; I ++ )...{
  15. Vals = new INTEGER (currval );
  16. Currval + = R. nextint (100) + 1;
  17. }
  18. Values = arrays. aslist (Vals );
  19. }
  20. Static long timelist (list lst )...{
  21. Long start = system. currenttimemillis ();
  22. For (INT I = 0; I <n; I ++ )...{
  23. Int Index = collections. binarysearch (LST, values. Get (I ));
  24. If (index! = I)
  25. System. Out. println ("*** error ***");
  26. }
  27. Return System. currenttimemillis ()-start;
  28. }
  29. Public static void main (string ARGs [])... {
  30. System. Out. println ("arraylist time consumed:" + timelist (New arraylist (values )));
  31. System. Out. println ("consumed list time consumed:" + timelist (New consumed list (values )));
  32. }
  33. }

 
The output I get is: arraylist time consumed: 15
Worker list time consumed: 2596

This result is not fixed, but the arraylist time is basically less than the time of the sorted list. Therefore, in this case, it is not recommended to use the consumer list. The random access policy used by the binary search method, while the random list does not support fast random access. The time consumed for Random Access to an external list is proportional to the size of the list. Correspondingly, the time consumed for random access in the arraylist is fixed.

Does this indicate that arraylist always performs better than javaslist? This is not necessarily the case. In some cases, the sorted list performs better than the arraylist. Some algorithms are more efficient in the sorted list. For example, when you use the collections. Reverse Method to reverse the list, its performance will be better.

Let's look at this example. We have a list to add and perform a lot of insert and delete operations on it. In this case, the sort list is a good choice. For example, in the next extreme example, we repeatedly insert an element at the beginning of a list:

Java code
  1. Package com. Mangocity. test;
  2. Import java. util .*;
  3. Public class listdemo {
  4. Static final int n = 50000;
  5. Static long timelist (list ){
  6. Long start = system. currenttimemillis ();
  7. Object o = new object ();
  8. For (INT I = 0; I <n; I ++)
  9. List. Add (0, O );
  10. Return System. currenttimemillis ()-start;
  11. }
  12. Public static void main (string [] ARGs ){
  13. System. Out. println ("arraylist time consumed:" + timelist (New arraylist ()));
  14. System. Out. println ("consumed list time:" + timelist (New consumed list ()));
  15. }
  16. }

In this case, the output result is: arraylist time consumed: 2463

Consumed list time: 15

This is opposite to the result of the previous example. When an element is added to the beginning of arraylist, all existing elements will be moved back, which means the overhead of data movement and replication. On the contrary, to add an element to the consumer list, the first step is simply to assign a record without the element, and then adjust the two connections. The overhead of adding an element at the beginning of the arraylist is fixed, and the overhead of adding an element at the beginning of the arraylist is proportional to the size of the arraylist.

Ii. space complexity
There is a private internal class in the tranquility list, which is defined as follows:

Java code
  1. Private Static class entry {
  2. OBJECT element;
  3. Entry next;
  4. Entry previous;
  5. }

 
One element in the reference list of each entry object, and the previous and next elements in the reference list. A listing list object with 1000 elements will have 1000 entry objects linked together. Each object corresponds to an element in the list. In this case, there will be a huge space overhead in a shortlist structure because it will store the information of the 1000 entity objects.

Arraylist uses a built-in array to store elements. The initial capacity of this array is 10. when the array needs to increase, the new capacity is obtained according to the following formula: new capacity = (old capacity * 3)/2 + 1, that is, the capacity increases by about 50% each time. This means that if you have an arraylist object that contains a large number of elements, a large amount of space will be wasted. This waste is caused by the arraylist's working method. If there is not enough space to store new elements, the array will have to be re-allocated so that new elements can be added. Re-allocating arrays will result in a sharp reduction in performance. If we know how many elements an arraylist will have, we can use the constructor to specify the capacity. We can also use the trimtosize method to remove the wasted space after the arraylist is allocated.

Iii. Summary
Arraylist and History List have their own advantages and disadvantages in terms of performance and have their own applicability. In general, they can be described as follows:

1. For arraylist and sort list, the overhead of adding an element at the end of the list is fixed. For arraylist, an item is mainly added to the internal array, pointing to the added element. Occasionally, the array may be re-allocated. For the sorted list, this overhead is uniform, allocate an internal entry object.

2. inserting or deleting an element in the middle of the arraylist means that all the remaining elements in the list will be moved. the overhead of inserting or deleting an element in the middle of the List is fixed.

3. the random list does not support efficient random element access.

4. The space waste of arraylist is mainly reflected in the reserved capacity space at the end of the list, while the space consumption of the List is reflected in that each of its elements consumes a considerable amount of space.

It can be said that when the operation is to add data after a column of data rather than in the front or middle, and needs to randomly access the elements, using arraylist will provide better performance; when you add or delete data in front or middle of a column of data and access the elements in the column in sequence, you should use the sort list.

Original address http://pengcqu.iteye.com/blog/502676

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.