List:
ArrayList
first, Let's look at how the source code of the Jdk's ArrayList Add method is Implemented:
Public boolean add (e e) {
Ensurecapacityinternal (size + 1); Increments modcount!!
elementdata[size++] = e;
Return true;
}
In the Aarrylist class there is a definition of the following array
/**
* The array buffer into which the elements of the ArrayList is Stored. --this is an array of stored ArrayList elements
* The capacity of the ArrayList is the length of this array buffer. The length of the---arraylist is the length of the array
*/
Private transient object[] elementdata;
In a comprehensive view, the implementation of the ArrayList underlying storage is implemented by an array
LinkedList
On the Source:
/**
* Pointer to First Node.
* Invariant: (first = = NULL && last = = Null) | |
* (first.prev = = null && first.item! = Null)
*/
Transient node<e> first;
/**
* Pointer to the last Node.
* Invariant: (first = = NULL && last = = Null) | |
* (last.next = = null && last.item! = Null)
*/
Transient node<e> last;
/**
* Links e as last Element.
*/
void Linklast (e E) {
Final node<e> L = last;
Final node<e> newNode = new node<> (l, E, null);
Last = newNode;
if (l = = Null)
First = newNode;
Else
L.next = newNode;
size++;
modcount++;
}
Inside the ArrayList there is such an inner class as the "node"
private Static Class Node<e> {
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;
}
}
Personal understanding: At this point you can guess LinkedList bottom implementation is a linked list, here the code just lists the end of the collection to add elements, see Linklast (e E) This method
Linklast method will generate node according to the parameter e, and then according to tail specific circumstances, modify the generation node Pre/next point, storage form is linked list
Map:
The underlying implementation of map is the array + linked list
Describe the process of map storage key-value:
When the key and value two objects are put to a map, they are encapsulated into a entry<k,v> entity object; the put procedure generates a hash code value (int type, different key may generate the same hash code) based on the K value
This hash code is treated as an index/subscript (index) of the array, each subscript of the array corresponds to a hash code, and a hash code corresponds to a linked list (the linked list stores objects with the same hash code)
For example: now to Map.put ("aa", "123"); "aa" corresponding to the hash code is 121
Map.put ("bb", "123"); "bb" corresponding to the hash code is also 121 to perform a put operation when the program will first find the subscript 121 (that is, The hash Value) of the list, and then through the linked list to store put in the object
Specific Code:
/**
* Associates The specified value with the specified key on this map.
* If The map previously contained a mapping for the key, the old
* Value is Replaced.
*
* @param key key with which the specified value was to be associated
* @param value value to being associated with the specified key
* @return The previous value associated with <tt>key</tt>
* <tt>null</tt> If there is no mapping for <tt>key</tt>.
* (A <tt>null</tt> return can also indicate that map
* Previously associated <tt>null</tt> with <tt>key</tt>.)
*/
Public V put (K key, v Value) {
if (key = = Null)
return Putfornullkey (value);
int hash = Hash (key.hashcode ());
int i = indexfor (hash, table.length); Find the corresponding subscript for the array
For (entry<k,v> e = table[i]; E = null; e = E.next) {//find the list object corresponding to the subscript in the array
Object k;
if (e.hash = = Hash && (k = E.key) = = Key | | key.equals (k))) {//the Overwrite operation when the original key already exists
V OldValue = e.value;
E.value = value;
E.recordaccess (this);
Return oldValue;
}
}
modcount++;
AddEntry (hash, key, value, i);//the entry that did not exist in the previous key is added after the list
Return null;
}
Set
Set is characterized by disorder, not repetition
Dummy value to associate with a Object in the backing Map
Private static Final Object PRESENT = new Object ();
Public boolean add (e e) {
return map.put (e, PRESENT) ==null;
}
The above is the HashSet source code, you can see the HashSet bottom is through the map to achieve, do not repeat: e hash value of the same time, map will take the form of coverage, so there will not be repeated
The disorder of map naturally leads to the disorder of HashSet (hash value seeking method?). Hashcode and Equals)
"finish the call" ....
Exploring the underlying implementation of the Java Collection class