Differences between vector, arraylist, and sorted list in Java

Source: Internet
Author: User
The SDK provides several Java. util. List Implementation interfaces for Ordered Sets. Three of them are most well-known: vector, arraylist, and sorted list. The performance difference between these List classes is a frequently asked question. In this article, I will discuss the functions of vector list and vector/array.

The SDK provides several Java. util. List Implementation interfaces for Ordered Sets. Three of them are most well-known: vector, arraylist, and sorted list. About this
The performance difference between some List classes is a frequently asked question. In this article, I want to discuss the performance differences between vector list and vector/arraylist.

To fully analyze the performance differences between these classes, we must know their implementation methods. Therefore, I will first briefly introduce the implementation features of these classes from the perspective of performance.

1. Implementation of vector and arraylist
Both vector and arraylist carry an underlying object [] array, which is used to save elements. When accessing an element through an index, you only need to access the elements of an internal array through an index:
Public
Object get (INT index)
{// First check whether the index is legal... this part of code is not displayed here
Return
Elementdata [Index];}

 

The internal array can be larger than the number of elements owned by the vector/arraylist object. The difference between the two can be used as the remaining space to quickly add new elements. With the remaining space, it is very easy to add elements. You only need to save the new elements to an empty position in the internal array, and then add the index value for the new free position:
Public
Boolean add (Object O)
{Ensurecapacity (size + 1); // elementdata [size ++] will be introduced later
= O; return true;
// Return value of list. Add (object}

Insert an element to any specified position in the Set (rather than the end of the Set). A little more complicated: All the array elements above the insertion point must move one forward before assigning values:
Public
Void add (INT index, object element)
{
// First check whether the index is legal... this part of code is not displayed here
Ensurecapacity (size + 1 );
System. arraycopy (elementdata,
Index, elementdata, index + 1,
Size-index );
Elementdata [Index] =
Element;
Size ++;
}

When the remaining space is used up, if more elements need to be added, the vector/arraylist object must replace its internal object [] array with a larger new array, and all array elements
To the new array. Depending on the SDK version, the new array is 50% or 100% larger than the original one (the code shown below expands the array by 100% ):
Public void
Ensurecapacity (INT mincapacity ){
Int oldcapacity = elementdata. length;
If
(Mincapacity> oldcapacity ){
Object olddata [] = elementdata;
Int
Newcapacity = math. Max (oldcapacity * 2, mincapacity );
Elementdata = new
Object [newcapacity];
System. arraycopy (olddata, 0, elementdata, 0,
Size );
}
}

The main difference between the Vector class and the arraylist class is synchronization. Except for two methods that are only used for serialization, none of the arraylist methods can be executed synchronously. On the contrary,
Most methods of Vector have synchronization capabilities, either directly or indirectly. Therefore, vector is thread-safe, but arraylist is not. This makes the arraylist
Vector fast. For some of the latest JVMs, the speed difference between the two classes is negligible: strictly speaking, for these JVMs, the speed difference between the two classes is less than the performance test of these classes.
The time difference shown in the test.

When accessing and updating elements through indexes, the implementation of vector and arraylist has excellent performance, because there is no
Other expenses. Unless the internal array space is exhausted, expansion is required. Otherwise, adding elements to the end of the list or deleting elements from the end of the list also has excellent performance. Inserting and deleting elements always requires
Copy the Array (two copies are required when the array must be extended first ). The number of elements to be copied is proportional to [size-Index], that is, between the insertion/deletion point and the last index location in the set.
Is proportional to the distance. During the insert operation, when an element is inserted to the beginning of the Set (index 0), the performance is the worst. When the element is inserted to the end of the Set (after the last existing element), the performance is the best. As the scale of the Set increases
The overhead of array replication increases rapidly because the number of elements that must be copied increases each insert operation.

II. Implementation of consumer list
The worker list is implemented through a list of nodes with two-way links. To access elements through indexes, you must search for all nodes until you find the target node:
Public
Object get (intindex ){
// First check whether the index is legal... this part of code is not displayed here
Entry E = header;
// Start Node
// Search for the forward or backward direction.
// Near decision
If (index <size/2 ){
For
(INT I = 0; I <= index; I ++)
E = E. Next;
} Else {
For (INT I = size;
I> index; I --)
E = E. Previous;
}
Return E;
}

It is easy to insert elements into the list: Find the node with the specified index and insert a new node immediately before the node:
Public void add (INT index, Object
Element ){
// First check whether the index is legal... this part of code is not displayed here
Entry E = header; // starting
Node
// Search for the forward or backward direction.
// Near decision
If (index <size/2 ){
For (int
I = 0; I <= index; I ++)
E = E. Next;
} Else {
For (INT I = size; I
> Index; I --)
E = E. Previous;
}
Entry newentry = new entry (element,
E, E. Previous );
Newentry. Previous. Next = newentry;
Newentry. Next. Previous
= Newentry;
Size ++;
}

Thread-safe consumer list and other sets
If you want
The SDK obtains a thread-safe consumer list. You can use a synchronization package to obtain the list from collections. synchronizedlist (list ).
One. However, the use of a synchronization package is equivalent to adding an indirect layer, which will bring a high performance cost. When the package passes the call to the encapsulated method, an additional method is required for each method.
The method encapsulated by the synchronous package is two to three times slower than the unencapsulated method. For complex operations such as search, the overhead of indirect calls is not very prominent. However, for simple methods,
For example, access or update functions, such overhead may have a serious impact on performance.

This means that, compared with the vector, the synchronized encapsulated list is
The performance is at a significant disadvantage because vector does not need to make any indirect calls for thread safety. If you want to have a thread-safe consumer list, you can copy
Sort list class and synchronize several necessary methods, so that you can get a faster implementation. This is equally effective for all other collection classes: only list and map have high
Effective thread security implementation (the vector and hashtable classes ). Interestingly, these two efficient thread security classes exist only for backward compatibility, not for performance considerations.

For accessing and updating elements through an index, the performance overhead of an indexed list is slightly higher, because accessing any index requires crossing multiple nodes. When an element is inserted
The higher the performance overhead of multiple nodes, the more overhead is required, that is, the overhead for creating node objects. In terms of advantages, the insert and delete operations implemented by the consumer list have no other overhead. Therefore, the insert
-The deletion overhead is almost entirely dependent on the distance between the insertion and deletion points and the end of the set.

Arraylist and vector are usually compared with the comparison list and synchronization.
The encapsulated consumer list has better performance. Even if you think the writable list may provide higher performance, you can modify the element addition method from
Arraylist strives for better performance, such as the order of elements in the flipped set.

In some cases, listing has better performance. For example, when a large number of elements need the same
When it is added to the beginning and end of a large set. However, in general, we recommend that you use the arraylist/Vector class first, only when they have obvious performance problems
The Sequence List is used only when the sequence list function can be changed.

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.