Collection is an interface to the Java collection, collection interfaces inherit iterable
Public interface Collection<e> extends iterable<e>
Do not have their own in the drawing of the diagram to find the internet has Big Brother draw the diagram below
There may be a wrong place, abstrctlist should inherit from
Public abstract class Abstractlist<e> extends abstractcollection<e> implements List<e>
One, List (interface)
1, ArrayList
Internally holding a object[] array
/** * Default initial capacity. */ private static final int DEFAULT_CAPACITY = 10; //Default Size /** * shared empty array instance used for empty instances. */ private static final object[] empty_elementdata = {}; //Object Array /** * 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 size of the ArrayList (the number of elements it contains) . * * @serial */ private int size; //array Size
all operations are based on this array to organize, generally not easy to traverse this array, generally more use system.arraycopy way to operate, The deleted action is sometimes dependent on the GC to be recycled, as follows:
/** * Removes the element at the specified position in this list. * shifts any subsequent elements to the left (subtracts one from their * indices) . * * @param index the index of the element to be removed * @return the element that was removed from the list * @throws indexoutofboundsexception {@inheritDoc} */ public e remove (Int index) { rangecheck (Index); modcount++; e oldvalue = elementdata (Index); int numMoved = size - index - 1; if (nummoved > 0) system.arraycopy (elementdata, index+1, elementdata, index, nummoved) //complete operation via a copy elementdata[--size] = null; // clear to let gc do its work return oldValue; }
2, LinkedList
LinkedList, the internal holds a series of node, node structure is as follows
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; } }
From the above you can see that node has saved the previous and last nodes information, and data, divided into three parts
The management of the nodes is as follows:
How many nodes does the transient int size = 0; //have /** * pointer to first node. * Invariant: (first == null && last == null) | | * ( First.prev == null && first.item != null) */ transient node<e> first; //head Node /** * Pointer to last node. * Invariant: (first == null && last = = null) | | * (last.next == null && last.item != null) */ transient node<e> last; // Tail node
All operations around the addition and deletion of the node, see a add all the actions
/** * Constructs a list containing the elements of the specified * Collection, in the order they are returned by the collection ' s * iterator. * * @param c the collection whose elements are to be placed into this list * @throws nullpointerexception if the specified collection is null */ public linkedlist (collection<? extends e> c) { this (); addall (c); } //Construction /** * appends all of the elements in the specified collection to the end of * this list, in the order that they are returned by the specified * collection ' s iterator. the behavior of this operation is Undefined if * the specified collection is modified while the operation is in * progress. ( note that this will occur if the specified collection is * this list, and it ' S nonempty.) * * @param c collection containing elements to be added to this list * @return {@code true} if this list changed as a result of the call * @throws nullpointerexception if the specified collection is null */ public boolean addall (Collection<? EXTENDS&NBSP;E>&NBSP;C) { return addall (size, &NBSP;C); } /** * inserts all of the elements in the specified collection into this * list, starting at the specified position. shifts the element * currently at that position (If any) and any subsequent elements to * the right (increases their indices). the new elements will appear * in the list in the order that they are returned by the * specified collection ' s iterator. * * @param index index at which to insert the first element * from the specified collection * @param c collection containing elements to be added to this list * @return {@code true} if this list changed as a result of the call * @throws indexoutofboundsexception {@inheritDoc} * @throws nullpointerexception if the specified collection is null */ public boolean addall (int INDEX,&NBSP;COLLECTION<?&NBSP;EXTENDS&NBSP;E>&NBSP;C) { checkpositionindex (Index); object[] a = C.toarray (); int numnew = a.length; if (numnew == 0) return false; node<e> pred, succ; if (index == size) { succ = null; pred = last; } else { succ = node (index); //returns the feature node at the index, which optimizes the process of finding from the head or tail. See Source code pred = succ.prev; } for (Object o : a) { @SuppressWarnings ("unchecked") e e = (E) o; node<e> newnode = new node<> ( Pred, e, null); if (pred == null) first = newnode; else pred.next = newNode; pred = newnode; } //Add Process if (succ == null) { last = pred; } else { pred.next = succ; succ.prev = pred; } size += numNew; modCount++; return true; }
3. Vector
ArrayList similar, the source code to add the Synchronized keyword, thread-safe
Second, Set (interface)
1, HashSet
HashSet internal actually holds a HashMap object, all operations are implemented by Operation Map, this map is no value, only key,
An internal HashMap object
private transient hashmap<e,object> map; Public HashSet (collection<? extends e> c) {map = new hashmap<> (Math.max ((int) (C.size ()/.75f) + 1, 16) ); Open a collection that is 4/3 larger than the original collection. Default AddAll (c); }
See a way to get an iterator
/** * Returns An iterator over the elements in this set. The elements * is returned in no particular order. * * @return an Iterator over the elements in this set * @see concurrentmodificationexception */Public Iter Ator<e> iterator () {return Map.keyset (). iterator (); }
2, TreeSet
TreeSet is also implemented via map.
private transient navigablemap<e,object> m; This is Navigablemap, which inherits from the SortedMap interface since1.6
The other method is to call the implementation of map, in the map to see
Third, Map (interface)
Map is a stand-alone interface that contains an entry interface for managing each node
Public interface map<k,v>{interface Entry<k,v> {}}
Java Collection Collection source code analysis (a)