The similarities and differences of ArrayList, Vector and LinkedList in Java

Source: Internet
Author: User

First, ArrayList

ArrayList is a type that can handle variable-length arrays, not limited to the "number" group, ArrayList is a generic class that can hold objects of any type. As the name implies, ArrayList is a list of arrays, so the interior uses an array to hold the object, because object is the parent class of all types, so ArrayList internally is an array class that has an object type. The common methods of the ArrayList class are add (), clear (), get (), indexOf (), remove (), sort (), ToArray (), toString (), and so on. At the same time ArrayList has a private class to implement the iterator interface, so you can use the iterator () method to get the ArrayList iterator, at the same time, there is a private class to implement the Listiterator interface, So ArrayList can also call the Listiterator () method to get the Listiterator iterator.

since ArrayList relies on arrays to store objects, it only encapsulates them, so the efficiency of some of its methods is O (n), which is almost the same as the normal array efficiency, except that the ArrayList is a mutable "array" and can hold all the objects specified.

In addition, because all methods of ArrayList are done by default under a single thread, ArrayList does not have thread safety. If you want to use it under multiple threads, you should call ArrayList using the static method Synchronizedlist () in the Colletions class.

Second, LinkedList

LinkedList can be seen as a doubly linked list, and all operations can be considered a doubly linked list operation because it implements the Deque interface and the list interface. Similarly, LinkedList is also thread insecure, and if used in a concurrent environment, it can also be called with the static Method Synchronizedlist () in the Colletions class .

In the internal implementation of LinkedList, instead of storing the data in an ordinary array, it uses nodes <Node> to hold the data, a node first pointing to the head of the chain and a node to the end of the list. Unlike the ArrayList can only add data at the end of the array, linklist can easily insert data in the chain header or at the end of the chain, or insert data before and after a specified node, and also provide a node that takes a chain header or a chain footer, or takes a node in the middle, and can also query for the presence of a node. The Add () method inserts data by default at the end of the linked list. In short, LinkedList provides a number of convenient ways to operate, and its insertion or addition method is significantly more efficient than the ArrayList type, but the query is less efficient because it is a doubly linked list.

Therefore, the biggest difference between LinkedList and ArrayList is that LinkedList is more flexible, and the efficiency of some methods is much higher than that of ArrayList counterparts, and for frequent data entry and exit, the operation needs to be flexible enough. It is recommended to use LinkedList; ArrayList can be used in cases where the array changes are small and mainly used for querying.

Third, Vector

Vector is also a variable-length array type similar to ArrayList, whose interior is also used to store data objects in arrays. It is noteworthy that the only difference between vector and ArrayList is that vector is thread-safe, that is, most of its methods contain keyword synchronized, so it is best to use ArrayList instead of vectors for single-threaded applications. Because this will be much faster (similar cases have StringBuffer and StringBuilder), and in multithreaded programs, in order to ensure data synchronization and consistency, you can use vectors instead of ArrayList to achieve the same function.

The similarities and differences of ArrayList, Vector and LinkedList in Java

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.