---- Collection set of source code analysis Interview

Collection Interface: single set, for storing an object a

. 1, List Interface: storing an ordered, repeatable data. -> "dynamic" array, replace the original array

2, the ArrayList: a main List interface implementation class, thread-safe, high efficiency, the use of the underlying Object [] elementData storage

2.1, ArrayList source code analysis: jdk7

ArrayList list = new ArrayList();//底层创建了长度是10的Object[]数组elementData
list.add(123); //elementData[0] = new Integer(123);
...
list.add(11);//如果此次的添加导致底层elementData数组数组容量不够,则扩容。默认情况下,扩容为原来的容量的1.5倍,同时需要将原有的数据复制到新的数组中。

Conclusion: It is recommended for use with a development argument constructor: ArrayList list = new ArrayList (int capacity)

2.2, jdk8 in ArrayList changes:

ArrayList list = new ArrayList();//底层Object[] elementData初始化为{},并没有创建长度为10的数组
list.add(123);//第一次调用add()时,底层才创建了长度10的数组,并将123添加到elementData中去
//后续的添加和扩容操作与jdk 7无异

Conclusion: to create objects in jdk7 ArrayList is similar to a hungry man single example, and create similar singleton lazy Objects in jdk8 ArrayList, the delay to create the array, saving memory.

. 3, the LinkedList: for frequent insertion, deletion, use of such higher efficiency than the ArrayList; underlayer using a doubly linked list memory

3.1, LinkedList source code analysis:

LinkedList list = new LinkedList();内部声明了Node类型的first和last属性,默认值为null
list.add(123);//将123封装到Node中,创建了Node对象。
其中,Node定义为:体现了LinkedList的双向链表的说法
    E item;
    Node<E> next;
    Node<E> prev;

    Node(Node<E> prev, E element, Node<E> next) {
        this.item = element;
        this.next = next;
        this.prev = prev;
    }

. 4, the Vector: As old List interface implementation class, thread-safe, low efficiency, the use of the underlying Object [] elementData storage

4.1, the Vector's source code analysis :

         jdk7 and jdk8 by Vector () constructor creates when objects are created with a length of the bottom of the array 10, in terms of expansion, the expansion to the original default twice the length of the array


Set Interface: storing random, non-repeating data

1, HashSet: as the main class that implements the Set interface, thread-safe, can store null values

2, a LinkedHashSet: as a subclass of HashSet: traversing its internal data to be traversed in order of addition. For frequent traversal operation, LinkedHashSet efficient than HashSet.

. 3, TreeSet: properties can be specified in accordance with the object added

3.1 TreeSet:

1, the data TreeSet added requirement is the object of the same class

2 Both Sort: natural ordering (implement the Comparable interface) and custom sorting (Comparator)

3, in the natural order, whether the two objects of the same comparison criteria as: compareTo () returns 0, no longer equals ().

4, custom ordering, whether the same standard to compare two objects to: compare () returns 0, no longer equals ().

Set source code analysis

Set in disorder: does not mean randomness. Data stored in the array so that the order is not added to the underlying data, but is determined based on a hash value of data

Nonrepeatability: guaranteed is determined, according to the additive elements can not return true equals (). That is: the same elements can add only one

The process of adding elements: to HashSet (array + list) as an example:

We add an element to a HashSet, the first call hashCode () method, calculates a hash value of the element where the element a, then the hash value calculated in the storage position HashSet underlying array (that is indexed by an algorithm position), determines whether there are elements in this array location:

    Case 1 -> If there is no other element in this position, the added element of a success.
                    If there are other elements b (or a plurality of elements present in the form of a linked list) in this position, a hash value of the element and the comparison element b is:
    Scenario 2 -> If the hash values are not identical, the element a is added successfully.
                            If the hash values are the same, and thus a need to call the class where the element equals () method:
                                Returns to true equals () method, adding a element fails.
    Case 3 -> equals () method returns false, then the element added a success.

For added successful in terms of Case 2 and Case 3: a element already present on the data at the specified index to the list stored.
jdk7: a element into an array, the elements of the original point.
jdk8: The original element in the array, point to elements of a
summary: at sixes and sevens

4.1 Set interface no additional define a new method, using the methods of the Collection are declared before.

4.2 Requirements:

    Rewrite the hashCode () and equals () to maintain consistency as possible: equal objects must have equal hash code
    rewrite method two tips: the object used as equals () method to compare Field, should be used hashCode value is calculated

Why rewrite hashCode method, there are 31 that number?

1. Select the coefficient of time to choose the largest possible factor, because if the calculated hash address the larger, so-called "conflict" less and look up efficiency will improve. (Reduce conflicts)

2, and 31 occupied 5bits, multiplying the resulting low probability of data overflow.

3,31 -1 can be represented by i * 31 == (i << 5), now has a lot to do inside the virtual machine-related optimization. (To improve the efficiency of the algorithm)

4, 31 is a prime number, the role of prime numbers is that if I use a number that is multiplied by this prime number, then the final result can only be out of the prime number itself is divisible by 1 and multiplicand as well! (Reduce conflicts)

Published 67 original articles · won praise 19 · views 9874

Guess you like

Origin blog.csdn.net/qq_41530004/article/details/103890766