The difference Arraylist and linkedlist (JDK source code reading)

PS: Recommend you go to look at this list data structure.

ArrayList and LinkedList can be said container class ordinary course of business development in the most commonly used, and at the same time, the difference between them is also a high incidence of the interview, although very simple, but we can not always say the integrity of today through their source code to read deepen understanding.

First, they look at the definition of the class can be found:

They are realized List interface, this interface is what did it?

This interface defines a list of basic operations, such as add,contains,indexof,removethe basic method, implemented by him for each implementation class.

So, when you just need to add a list of routine removal of the search operation, the ArrayList and LinkedList in experience (without regard to performance) basically no difference, you do not even care about his internal implementation, but call some of the List interface methods on ok.

Then their concrete realization of what difference does it make?

Following their usual method of reading the source code.

ArrayList

Member variables

ArrayList variables has two members, can be seen in FIG., An array of Object, an int size, used to define the size of the array.

get () method

First check the incoming index, and returns the value in the index of an array.

add () method

First make sure that enough capacity, then the object is newly added on the end of the array.

remove () method

First, to ensure sufficient capacity, then calculate the required amount of movement, for example, size = 10, to remove the element index = 5, the four elements need to move back and then call System.arraycopy()the method, the four sequentially moved forward end of the array one, then the last position in the array is null.

LinkedList

Member variables

LinkedList property itself is relatively small, there are three, one is size, indicating that the current number of nodes; one is the first representative of the first node; the last one is the last representative of a node.

get () method

First check the incoming index is legitimate, then call the node(index)method, check node()method.

Determining whether the index value is greater than half of the total.

If less than backwards traversal from the first node, node until it finds the index, and returns the value of the node.

If so, from the last node to traverse forward until you find the index node, and returns the value of the node.

add () method

add method, called direct linklast method, the value passed as the last link in a node list.

remove () method

What approach is the idea remove it? Traverse the list from the beginning, when the node is found to be deleted, he will be deleted. Delete way to do that? The front and rear node linked node, similar to the following:

Compared

By a conventional method can be found in the above

1.ArrayList storage elements using an array, so the query faster, the element can be directly returned to the position, time complexity is O (1); and using a doubly linked list LinkedList storage element, when the end of the query from scratch or traverse to query elements, time complexity is O (n / 2);

2. When the storage problems or because, in the ArrayList insert or delete, the need to move all the elements after insertion position, slow, time complexity is O (n). And LinkedList just need to find the location, movement "pointer" to the time complexity is O (1).

in conclusion

In fact, in the daily development, ArrayList more popular, and can do a lot of tasks, but there are still some special scenarios for use LinkedList. Their usage scenario is as follows:

When you query the list of more that get the elements of a location, priority should use ArrayList; when you need a list of frequent deletion and additions rarely used queries, priority use LinkedList;

Precautions!

1. The above conclusion applies to the general situation, not necessarily in line with some extreme cases. Frequent insert data such as near the end of the array, ArrayList also faster than LinkedList.

2.LinkedList space larger than the ArrayList used, because in essence, the ArrayList elements stored in each position, and the storage element LinkedList + + behind the front node node.

Spread

We know that ArrayList and LinkedList are all size, then when too many elements to add, and how they expansion it?

ArrayList:

ArrayList using storage array elements, so when expansion is:

It can be seen that after each expansion of the size of 1.5 times before.

int newCapacity = oldCapacity + (oldCapacity >> 1);
复制代码

And then there are all the elements of a copy operation, this operation is very time-consuming.

LinkedList:

Because LinkedList is a doubly linked list, so no expansion mechanism, the elements can be added directly before or after.

Therefore: When using ArrayList, if you can estimate the size, preferably directly define the initial capacity, this can save additional costs caused by frequent expansion.

Constructor initializes capacity is defined as:

postscript

In fact, I wanted to write this for a long time, has been putting off, finally recalled the horror when the interview was dominated ArrayList and LinkedList. (Like asking, keep asking (recruit school)). Therefore build on the progress they read the source code and recorded. I believe under review often will not be trapped here, but also make the daily work of the encoding level slightly improved.

Finish.





ChangeLog

2018-10-11 completed

Above are all personal income and think, correct me if wrong welcomed the comments section.

Welcome to reprint, please sign, and retain the original link.

Contact E-mail: [email protected]

More study notes, see the individual blog ------> Huyan ten

Guess you like

Origin juejin.im/post/5cff45ac6fb9a07ecd3d52bf