JDK List analysis, JDK List

Source: Internet
Author: User

JDK List analysis, JDK List

List is also used in normal development, but it is generally interface-oriented. Therefore, List type is used, but ArrayList or listlist is used for related operations. This article mainly describes the JDK source code ArrayList and javaslist.

The underlying layer of ArrayList is maintained in the form of arrays. The main methods include add, remove, size, contains, toArray, and other related methods. Add is actually very simple. The bottom layer is to increase the length of the array and then put the elements to be added into the array. The implementation method is as follows: this. elementData [(this. size ++)] = paramE. Before this operation, the length of the array is increased by 1 to add the newly added elements to the current array. This adds the List function. However, if you want to insert data to a specified position, the operation is more complicated. It moves the current array one bit backward, uses System. arraycopy to copy the following elements, and inserts the specified elements. Such time is the cost. Therefore, when many elements are inserted, the sequence list is used first, which will be introduced later. The remove principle is the same. However, remove has an overload. When it is a subscript int, it will copy the specified element, and then set the specified element to null, so that the function of deleting the specified subscript is realized. But the reload is different. It receives the Object type. In this way, you can remove objects regardless of the type stored in the list. When this method is used, it will search for qualified elements, obtain the subscript, and then use the firstRemove method to remove the specified element. Of course, this method can only remove the first element. If you want to remove all matching elements, You have to traverse them or use the removeAll method. It should be noted that the removeAll receiving parameter is Collection. As for the size in the ArrayList, it is very simple. Because the bottom layer is an array, you only need to return the size of the current array, which is the length of this list. Contains returns the subscript of the current array. The indexOf method is used. If not, indexOf returns-1. The toArray method returns a list Array. Use copy in Array directly. In fact, there is an important method in ArrayList, that is, the Iterator. However, the Iterator is in the parent class javasactlist and placed in the internal class Itr. When used, an Iterator is directly defined, then, call the iterator method. This method returns the object of the internal class, And this object can be used for iteration, including the method next and hasNext. This is the most commonly used, and focuses on the analysis and implementation here. We can first define an Iterator of the interface type and use iterator () to directly obtain the object. This method returns an Itr object (internal class, which exists in the parent class ), this internal class contains next and hasNext, and then you can use this object to traverse the array. HasNext is used to determine whether there are still elements. This method uses the cursor concept. If the current cursor is equal to the current array size, true or false is returned. Use hasNext to take out the value and move the cursor one bit backward. In this way, the array can be traversed. Of course, this method is invisible in the parent class and in the ArrayList. The iterator uses a typical interface-oriented programming idea. It only defines the interface type and then calls related methods in the implementation class. (Interface-oriented programming will be analyzed in the future ). After the iterator is used in the API, the list cannot be changed. The specific reason is not found in JDK. The preliminary analysis is based on the cursor. (ArrayList is NOT thread-safe. As mentioned in the API, if it is thread-safe, you only need to put it in Collections. synchronizedList is rarely used at work. After reading the JDK source code, the method is to put the list into the SynchronizedList code block. The specific implementation is not visible, and I rarely use it anyway.

Except list and ArrayList have the same functions, but their implementation methods are quite different. At the underlying layer, the system uses linked lists and internal Entry classes. The Entry includes element, next, and previous. When the list is instantiated, values are assigned to the three variables. All subsequent operations are centered around these three variables. The add, remove, size, and contains methods are also analyzed here. Before introducing add, we will introduce addBefore. AddBefore encapsulates the current element into an Entry, and then the next and previous values are assigned again, so that the linked list is moved backward, and elements are added to the linked list. If you are interested in the linked list, you can check the data structure by yourself. The add method calls addBefore, but uses the header for next in the linked list, so that the element is added to the linked list. The add method of this list has a huge cost, because it needs to move the next and previous of the linked list, without the simplicity of the ArrayList, and assign values directly. The remove method has similar implementation principles and is also used to operate the linked list. The size method returns the length of the current linked list. The contains method is used to traverse the linked list, move the linked list step by step, and perform matching. The underlying implementation of the toArray method is to take out the elements of the linked list, put them into an array, and return the array. However, the system returns an array directly as it would in the ArrayList. The iterator of the aggregate list is similar to that of the ArrayList iterator, but the underlying implementation is different. For example, hasNext uses this. nextIndex is used to determine whether the next element exists, rather than checking whether an element exists in the array. I need to take a deeper look at the listing list. Today I only read some superficial knowledge and will add it later.

This is what we have analyzed tonight. In fact, there are still many things to watch in these two categories. We hope that the majority of users will provide good analysis and learn from each other.

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.