Common Set ArrayList shortest parsing and set arraylist Parsing

Source: Internet
Author: User

Common Set ArrayList shortest parsing and set arraylist Parsing

First, let's take a look at the definition code of ArrayList in java:

public class ArrayList<E> extends AbstractList<E>        implements List<E>, RandomAccess, Cloneable, java.io.Serializable{    private static final long serialVersionUID = 8683452581122892189L;    /**     * Default initial capacity.     */    private static final int DEFAULT_CAPACITY = 10;    /**     * Shared empty array instance used for empty instances.     */    private static final Object[] EMPTY_ELEMENTDATA = {};    /**     * The array buffer into which the elements of the ArrayList are stored.     * The capacity of the ArrayList is the length of this array buffer. Any     * empty ArrayList with elementData == EMPTY_ELEMENTDATA will be expanded to     * DEFAULT_CAPACITY when the first element is added.     */    private transient Object[] elementData;

The information provided by this Code:
First, ArrayList implements the RandomAccess interface, so the query will be fast.

Second, ArrayList implements the serialization and Cloneable interfaces. The reason should be that the clone operation can be performed. However, it is controversial that this operation is not because it is the same object, but I did not test it myself. Controversial points...

Third, ArrayList uses arrays at the underlying layer. The default length of this array is 10.

Well, the above Code seems to be only able to get these conclusions. However, since it is an ArrayList, the length can be increased. In this case, the array must be variable. If you look down the code of the definition file of Arraylist, this is indeed the case. However, since it is a simple parsing, we will not analyze all the code one by one. The conclusion is that the ArrayList underlying layer stores elements using a variable array. The initial length of this array is 10, in addition, you can expand the capacity as needed.

One of our concerns about expansion is: How big is each expansion? If a scale-out is too large, it will cause a waste of space. If the scale-out is not enough, the scale-up operation will occur frequently, which will also consume performance. For this problem, the designer selects a discount size for us. If the original size indicates that the size after expansion is: size * 3/2 + 1.

Next, let's talk about the underlying layer Used for ArrayList resizing, that is, copying an array: Arrays. copyOf (array0, newSize); array-based copy, the operation process of deleting an element in ArrayList can be divided into the following three steps:

① Delete the elements at the specified position of the array. ② Move all the elements behind the array to a forward position. ③ The last element is set to null so that the garbage collection mechanism can recycle space.

Adding Elements to an array is similar to deleting an array. Based on this situation, we can find two disadvantages of ArrayList:

1. When deleting an element, it involves an element copy. If there are many elements to be copied, it will be performance-consuming.

2. When an element is inserted, it involves an element copy. If there are many elements to be copied, it will be more time-consuming. Since ArrayList can become a widely used set, it is impossible to write the advantages because of these two shortcomings:

1. ArrayList is implemented in arrays at the underlying layer. It is a random access mode. In addition, it implements the RandomAccess interface. Therefore, it is very fast to search for get.

2. It is very convenient for ArrayList to add an element in sequence, just adding an element to the array.

Therefore, ArrayList is suitable for sequential addition and random access, while list-like is suitable for inserting and deleting a large number of set operations. It can only be said that in most cases, the specific efficiency is not all the way, because it is an array copy operation, the ArrayList for the deletion and addition of elements with relatively low back will be much faster than the sort list.

Last tips:

① ArrayList is thread unsafe. If thread security is not required, you can use Collections. synchronizedList to perform the following operations:

List<String> list= Collections.synchronizedList(list);list.add("aaa");list.add("bbb");for (int i = 0; i < list.size(); i++){    System.out.println(list.get(i));}

② For set operations, if the RandomAccess interface (such as ArrayList) is implemented, use the for (int I = 0; I <size (); I ++) method for traversal, this is twice the efficiency of an enhanced for loop. Without RandomAccess (for example, random list), the efficiency of using a common for loop is times lower than that of using an iterator. Therefore:

   if (list instance of RandomAccess) {        for(int m = 0; m < list.size(); m++){}    }else{        Iterator iter = list.iterator();        while(iter.hasNext()){}    })

  

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.