Java basic knowledge trap (7)
The relationship between HashSet and HashMap was discussed last time. The HashMap contains the following internal sentence: static final float DEFAULT_LOAD_FACTOR = 0.75f; this sentence indicates a constant, the function is to re-construct an array of 2 times the size of the container when the number of containers reaches 0.75%. If the two are collections, let's look at other collection classes today, such as ArrayList, Vector, and sorted list. First, check the source code of ArrayList. These collection classes are in java. util package; Structure: private transient Object [] elementData; public ArrayList (int initialCapacity) {super (); if (initialCapacity <0) throw new IllegalArgumentException ("Illegal Capacity: "+ initialCapacity); this. elementData = new Object [initialCapacity];} public ArrayList () {this (10) ;}you can see that when we use new ArrayList, in essence, an array of Object objects with a length of 10 is initialized by default. I understand that the interior is implemented using arrays. Let's continue to look at what happens when we add them? Look at its code: public boolean add (E e) {ensureCapacity (size + 1); // Increments modCount !! ElementData [size ++] = e; return true;} public void ensureCapacity (int minCapacity) {modCount ++; int oldCapacity = elementData. length; if (minCapacity> oldCapacity) {Object oldData [] = elementData; int newCapacity = (oldCapacity * 3)/2 + 1; if (newCapacity <minCapacity) newCapacity = minCapacity; elementData = Arrays. copyOf (elementData, newCapacity) ;}} we can see that when you add an object, you must first determine whether the inserted position of the small mark is greater than the length of the array. Instantiate an array and assign it to elementData. When the array is re-instantiated, the length is calculated according to the current length * 3/2 + 1, that is, it is determined every time the array is added, if a large number of element objects (hundreds of thousands) are inserted at a time, we can imagine that when 10 elements are inserted, an array will be re-instantiated, and again at 16 times, at the time of 25, the performance should be too bad, so we can directly call the construction with parameters to improve the performance when we know the length of the array, for example: arrayList list = new ArrayList (10000); then let's take a look at its query method: public boolean contains (Object o) {return indexOf (o)> = 0 ;} public int indexOf (Object o) {if (o = null) {for (int I = 0; I <size; I ++) if (elementD Ata [I] = null) return I;} else {for (int I = 0; I <size; I ++) if (o. equals (elementData [I]) return I;} return-1;} we can see that the entire array is traversed during the query! Check the removal code: public E remove (int index) {RangeCheck (index); modCount ++; E oldValue = (E) elementData [index]; int numMoved = size-index-1; if (numMoved> 0) System. arraycopy (elementData, index + 1, elementData, index, numMoved); elementData [-- size] = null; return oldValue;} private void RangeCheck (int index) {if (index> = size) throw new IndexOutOfBoundsException ("Index:" + index + ", Size:" + size);} The removed The Code also first needs to judge whether the small mark has exceeded, otherwise it will intercept the array. From this writing aspect, we can see that ArrayList uses subscript traversal directly when searching. When adding and deleting, We need to reconstruct a new array, which consumes a lot of performance! Now let's take a look at the Vector class. Let's first look at its construction: protected Object [] elementData; public Vector (int initialCapacity) {this (initialCapacity, 0);} public Vector () {this (10);} public Vector (int initialCapacity, int capacityIncrement) {super (); if (initialCapacity <0) throw new IllegalArgumentException ("Illegal Capacity:" + initialCapacity ); this. elementData = new Object [initialCapacity]; this. capacityIncrement = capacityIncrem Ent ;}we can see that this Vector is also implemented using an Object array. initialCapacity shows the length of the Set array and capacityIncrement. Let's take a look at its annotations: /*** The amount by which the capacity of the vector is automatically * incremented when its size becomes greater than its capacity. if * the capacity increment is less than or equal to zero, the capacity * of the vector is doubled each time it needs to grow. ** @ serial */I am not good at English. It's amazing. It's a bit difficult for us to see what we use: pr Ivate void merge (int minCapacity) {int oldCapacity = elementData. length; if (minCapacity> oldCapacity) {Object [] oldData = elementData; int newCapacity = (capacityIncrement> 0 )? (OldCapacity + capacityIncrement): (oldCapacity * 2); if (newCapacity <minCapacity) {newCapacity = minCapacity;} elementData = Arrays. copyOf (elementData, newCapacity) ;}} from this code, we can see that when capacityIncrement> 0 is set, when the object is added, the length of the new array is used when the new array is re-constructed. Otherwise, it is twice the length of the original array. OK! Poor English! Next, let's take a look at how to add and delete it. Code: public synchronized boolean add (E e) {modCount ++; ensureCapacityHelper (elementCount + 1); elementData [elementCount ++] = e; return true ;} public synchronized E remove (int index) {modCount ++; if (index> = elementCount) throw new ArrayIndexOutOfBoundsException (index); Object oldValue = elementData [index]; int numMoved = elementCount-index-1; if (numMoved> 0) System. arraycopy (elementData, in Dex + 1, elementData, index, numMoved); elementData [-- elementCount] = null; return (E) oldValue;}. The synchronized keyword is displayed here, which is synchronous management. When adding Add, first determine whether to re-construct the new array, and then assign values. When deleting the array, first determine whether the subscript exceeded. Otherwise, the array is intercepted directly. This is the same as the ArrayList! It can be seen that the Vector is also implemented using arrays, But synchronization can be used to add and delete some methods, even in multiple threads. This performance is a little bit better than that of ArrayList, But if you consider the multi-threaded environment, you should use this to ensure that the query is correctly executed. Next, let's take a look at the shortlist and look at its code: public class shortlist <E> extends AbstractSequentialList <E> implements List <E>, Deque <E>, Cloneable, java. io. serializable from this we can see that this sort list actually implements Deque. You can see that this annotation is a two-way circular linked list. It turns out that the linked list should be known to everyone. The linked list will not be faster when inserting data to delete data, because it does not need to remove data and directly modify the pointer, however, it is also difficult to query the node. It is time-consuming to search the node from the bottom or on the Internet! Public boolean add (E e) {addBefore (e, header); return true;} private Entry <E> addBefore (E e, Entry <E> entry) {Entry <E> newEntry = new Entry <E> (e, entry, entry. previous); newEntry. previous. next = newEntry; newEntry. next. previous = newEntry; size ++; modCount ++; return newEntry;} private E remove (Entry <E> e) {if (e = header) throw new NoSuchElementException (); E result = e. element; e. previous. n Ext = e. next; e. next. previous = e. previous; e. next = e. previous = null; e. element = null; size --; modCount ++; return result;} from the code above, we can see that adding and deleting all operations are only performed on the header node, which greatly improves the performance. Private transient Entry <E> header = new Entry <E> (null, null, null); private transient int size = 0; public writable list () {header. next = header. previous = header;} private static class Entry <E> {E element; Entry <E> next; entry <E> previous ;............} from this part of the code, we can see that the header is used as the link node, and the header contains the previous and next nodes. The first and last nodes constitute the entire chain table loop.