Arraylist,vector and LinkedList different detailed

Source: Internet
Author: User
Original address: https://my.oschina.net/zzw922cn/blog/491631 Abstract: This blog details the similarities and differences between the three arraylist/linkedlist/vector. 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 suggests, ArrayList is an array list, so its interior uses an array to hold the object, because object is the parent of all types, so ArrayList has an object-type array class for storing objects inside. Common methods for ArrayList classes are Add (), clear (), get (), indexOf (), remove (), sort (), ToArray (), toString (), and so on. At the same time, there is a private class inside the ArrayList to implement the iterator interface, so the iterator () method can be used to get ArrayList iterators, and a private class implements the Listiterator interface. So ArrayList can also call the Listiterator () method to get the Listiterator iterator.

Because ArrayList relies on arrays to hold objects, it is only encapsulated, so some of its lookup methods are efficient O (n), similar to normal array efficiency, except that the ArrayList is a variable "array" and can hold all the specified objects.

In addition, because all methods of ArrayList are performed by default under a single thread, ArrayList does not have thread security. If you want to use multithreading, you should use the static method Synchronizedlist () in the Colletions class to call ArrayList. Second, LinkedList

LinkedList can be regarded as a two-way linked list, all operations can be considered as a two-way linked list operation, because it implements the Deque interface and list interface. Similarly, LinkedList is also thread-insecure, and if used in a concurrent environment, the LinkedList is also invoked using the static method Synchronizedlist () in the Colletions class.

In the internal implementation of the LinkedList, the data is not stored in a normal array, but the node <Node> is used to store the data, and there is a node that points to the header of the list and a node that points to the end of the list. Unlike ArrayList, you can only add data at the end of an array. linklist can easily insert data at the end of a linked table or chain, or insert data before and after a specified node, and also provide a node to take the chain header or the end of the chain, or to take a node in the middle, or to query whether a node exists. The Add () method inserts data at the end of the list by default. In short, LinkedList provides a large number of convenient operations, and its insertion or increase method is significantly more efficient than the ArrayList type, but the query is less efficient, because it is a two-way linked list.

Therefore, the biggest difference between LinkedList and ArrayList is that the LinkedList is more flexible, and the efficiency of some methods is much higher than that of the ArrayList corresponding method, for the case of frequent data access and the need for the operation to be flexible enough, Recommended use of LinkedList; For array changes are not small, mainly used to query the case, you can use ArrayList. Third, Vector

The vector is also a variable-length array type similar to ArrayList, and its interior uses an array to hold the data object. It is noteworthy that the only difference between vector and ArrayList is that the vector is thread safe, that most of its methods contain keyword synchronized, so it is best to use ArrayList instead of vector for a single thread application. Because this is much faster (similar to StringBuffer and StringBuilder), in multithreaded programs, vectors can be used instead of ArrayList to achieve the same function in order to ensure data synchronization and consistency.

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.