What is the difference between ArrayList, list, and Vector ., Arraylistparts list

Source: Internet
Author: User

What is the difference between ArrayList, list, and Vector ., Arraylistparts list

The ArrayList, inclulist, and Vestor classes all implement the java. util. List interface, but they have different features, mainly as follows:

I. Synchronization

ArrayList and inclulist are not synchronized, while Vestor is synchronized. If thread security is not required, you can use ArrayList or javaslist to save the cost for synchronization. But in the case of multithreading, sometimes we have to use Vector. Of course, you can also package ArrayList and history list in some ways to synchronize them, but the efficiency may be reduced.

Ii. Data Growth
In terms of the internal implementation mechanism, both ArrayList and Vector are stored in the Objec array format. When you add elements to these two types, if the number of elements exceeds the current length of the internal array, both of them need to extend the length of the internal array, by default, Vector automatically doubles the length of the original array, and ArrayList is 50% of the original length. Therefore, the space occupied by this set is always larger than what you actually need. Therefore, if you want to save a large amount of data in the collection, using Vector has some advantages, because you can avoid unnecessary resource overhead by setting the initialization size of the collection.

Iii. Efficiency of searching, inserting, and deleting objects

In ArrayList and Vector, retrieving an object from the specified position (using index) or inserting or deleting an object at the end of the set takes the same time, can be expressed as O (1 ). However, if an element is added or removed from another position in the Set, the time consumed will grow linearly: O (n-I), where n represents the number of elements in the set, I indicates the index location where the element is added or removed. Why? When performing the preceding operations, all the elements after the I and I elements in the set must perform the (n-I) object displacement operation.
In the sorted list, the time spent on inserting or deleting elements at any position in the set is the same-O (1), but it is slow when indexing an element, is O (I), where I is the index location.
----------------------------------------
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
Package com. mangocity. test;
Import java. util. Collections list;
Import java. util. List;
Import java. util. Random;
Import java. util. ArrayList;
Import java. util. Arrays;
Import java. util. Collections;
Public class TestList ...{
Public static final int n= 50000;

Public static List values;

Static ...{
Integer vals [] = new Integer [N];

Random r = new Random ();

For (int I = 0, currval = 0; I <N; I ++ )...{
Vals = new Integer (currval );
Currval + = r. nextInt (100) + 1;
}

Values = Arrays. asList (vals );
}

Static long timeList (List lst )...{
Long start = System. currentTimeMillis ();
For (int I = 0; I <N; I ++ )...{
Int index = Collections. binarySearch (lst, values. get (I ));
If (index! = I)
System. out. println ("*** error ***");
}
Return System. currentTimeMillis ()-start;
}
Public static void main (String args [])... {
System. out. println ("ArrayList time consumed:" + timeList (new ArrayList (values )));
System. out. println ("consumed list time consumed:" + timeList (new consumed list (values )));
}
}

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
Package com. mangocity. test;

Import java. util .*;
Public class ListDemo {
Static final int N = 50000;
Static long timeList (List list ){
Long start = System. currentTimeMillis ();
Object o = new Object ();
For (int I = 0; I <N; I ++)
List. add (0, o );
Return System. currentTimeMillis ()-start;
}
Public static void main (String [] args ){
System. out. println ("ArrayList time consumed:" + timeList (new ArrayList ()));
System. out. println ("consumed list time:" + timeList (new consumed list ()));
}
}
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 the ArrayList, all existing elements are moved back, this 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. spatial complexity
There is a private internal class in the tranquility list, which is defined as follows:

Java code
Private static class Entry {
Object element;
Entry next;
Entry previous;
}

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 reconfigured to add new elements. 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 respective advantages and disadvantages in terms of performance. 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 empty space waste of ArrayList is mainly reflected in the reserved capacity space at the end of the list, while the space consumption of the sorted 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 before or between a column of data and access the elements in the column in sequence, you should use the sequence list.

Therefore, if you only search for elements at a specific position or add or remove elements only at the end of the set, you can use Vector or ArrayList. If you want to insert or delete other operations at the specified position, you are advised to select the recommended list.

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.