Dark Horse Programmer---ArrayList, Vector, linkedlist the difference and advantages and disadvantages?

Source: Internet
Author: User

------- Android Training , Java training , look forward to communicating with you! ----------

First, the synchronization of

arraylist,linkedlist are not synchronized, while vestor are synchronous. So if thread safety is not required, you can use ArrayList or LinkedList to save the overhead of synchronizing. But in the case of multithreading, sometimes you have to use vectors. Of course, there are some ways to package arraylist,linkedlist, so that they can also be synchronized, but the efficiency may be reduced.
second, the variability of
from the internal implementation mechanism, ArrayList and vectors are stored using the array form of OBJEC. When you add elements to both types, if the number of elements exceeds the current length of the internal array, they all need to extend the length of the internal array, and vector automatically grows the array length by default, ArrayList is the original 50%. So in the end you get this collection that takes up more space than you actually need. So if you're going to save a lot of data in a collection then there are some advantages to using vectors, because you can avoid unnecessary resource overhead by setting the initialization size of the collection.
Third, operating efficiency
in ArrayList and vectors, retrieving an object from a specified position (with index), or inserting or deleting an object at the end of the collection, can be represented as O (1). However, if an element is added or removed elsewhere in the collection, the time spent will grow linearly: O (n-i), where n represents the number of elements in the collection, and I represents the index position at which the element is incremented or removed. Why is that? It is assumed that all elements after the first and second elements of the collection will perform the displacement of (N-i) objects when performing the above operations.
In LinkedList, the time it takes to insert or delete an element anywhere in the collection is the same as-O (1), but it is slower when indexing an element, O (i), where I is the index position.
ArrayList and LinkedList are two collection classes that store a series of object references (references). For example, we can use ArrayList to store a series of string or integer. So what's the difference in performance between ArrayList and LinkedList? When should I use ArrayList when should I use LinkedList?
ArrayList and LinkedList have their own advantages and disadvantages in performance, and each has its own applicable place, which can be described as follows:
1. For ArrayList and LinkedList, the overhead of adding an element at the end of the list is fixed. For ArrayList, the main point is to add an entry in the internal array, pointing to the element being added, which may occasionally cause the array to be redistributed, whereas for LinkedList, the overhead is uniform, allocating an internal entry object.
2. Inserting or deleting an element in the middle of a ArrayList means that the remaining elements in the list will be moved, while the overhead of inserting or deleting an element in the middle of the linkedlist is fixed.
3. LinkedList does not support efficient random element access.
4. ArrayList space waste is mainly reflected in the end of the list to reserve a certain amount of space, and the space cost of LinkedList is reflected in its every element needs to consume considerable space
It can be said that using ArrayList provides better performance when the action is to add data after a column of data rather than in front or in the middle, and you need to randomly access the elements in it, and when your action is to add or delete data before or in the middle of a column of data, And in order to access the elements in it, you should use LinkedList.
So, if you just look for elements in a particular location or only add and remove elements at the end of the collection, you can use either a vector or a ArrayList. If you are inserting or deleting other specified locations, it is best to select LinkedList

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Dark Horse Programmer---ArrayList, Vector, linkedlist the difference and advantages and disadvantages?

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.