Java. the container types (data structures) commonly used in lang are reviewed. These are the basis and can be easily found during normal use. Some methods are probably known, but it is always confusing. If you can remember the important methods and be proficient in using them, it will make the coding process much easier. The other is the implementation mechanism. We should be familiar with the implementation mechanism of common data structures.
In addition, I have sorted the concurrent containers in this article.
Queue
The difference between add and offer is that when the upper limit is reached, add throws an exception and offer returns false;
The difference between remove and poll is that when the queue is empty, the former throws an exception and the latter returns null;
Both element and peek return the queue header element, but the former fails without throwing an exception, and the latter returns null.
Boolean add (E e );
Boolean offer (E e );
E remove ();
E poll ();
E element ();
E peek ();
PriorityQueue, which is internally implemented as a heap carried by the Object [] queue.
Deque: double-ended queue. Based on the Queue, the following methods are added:
Void addFirst (E e );
Void addLast (E e );
Boolean offerFirst (E e );
Boolean offerLast (E e );
E removeFirst ();
E removeLast ();
E pollFirst ();
E pollLast ();
E getFirst ();
E getLast ();
E peekFirst ();
E peekLast ();
Boolean removeFirstOccurrence (Object o );
Boolean removeLastOccurrence (Object o );
ArrayDequeue: array implementation. The expansion policy doubles the capacity.
List
Boolean add (E e );
Boolean remove (Object o );
E get (int index );
E set (int index, E element );
Void add (int index, E element );
E remove (int index );
ArrayList. The scaling policy is (oldCapacity * 3)/2 + 1.
Consumer List, which not only implements the self-List interface, but also implements the Deque interface.
Vector, which implements the self-List interface. The internal implementation is an array, thread security, and the resizing policy is (capacityIncrement> 0 )? (OldCapacity + capacityIncrement): (oldCapacity * 2 ).
Stack is a subclass of Vector, and some Stack methods are added:
E push (E item)
E pop ()
E peek ()
Boolean empty ()
Map
Boolean containsKey (Object key );
Boolean containsValue (Object value );
V get (Object key );
V put (K key, V value );
V remove (Object key );
Set <K> keySet ();
Collection <V> values ();
Set <Map. Entry <K, V> entrySet ();
SotedMap interface, where the key is an ordered map:
SortedMap <K, V> subMap (K paramK1, K paramK2 );
SortedMap <K, V> headMap (K paramK );
SortedMap <K, V> tailMap (K paramK );
K firstKey ();
K lastKey ();
The subinterface NavigableMap provides some methods to find the key before or after a key. Here, floorKey/celingKey indicates that the relationship is less than or equal to/greater than or equal to, and lower/higher indicates that the relationship is strictly less than/greater:
Map. Entry <K, V> floorEntry (K key)
K floorKey (K key)
Map. Entry <K, V> ceilingEntry (K key)
K ceilingKey (K key)
Map. Entry <K, V> lowerEntry (K key)
K lowerKey (K key)
Map. Entry <K, V> higherEntry (K key)
K higherKey (K key)
TreeMap is the direct implementation subclass of NavigableMap, and the internal implementation is a red/black tree.
The EnumMap structure is <K extends Enum <K>, V>. It is implemented through a K [] keyUniverse and an Object [] vals internally.
HashMap is implemented by arrays and linked lists. When threshold = capacity * loadFactor is reached, the expansion policy is numKeysToBeAdded/loadFactor + 1.
HashTable, which implements self-Dictionary and Map. The methods are thread-safe. The put method of HashTable. value cannot be empty. This is a different method from HashMap. The hash method for finding buckets is different. The threshold calculation logic is the same, but its scaling policies are different: oldCapacity * 2 + 1. HashTable, HashMap, and HashSet are often put together for comparison.
Properties is a subclass of HashTable, and the method thread is secure.
IdentityHashMap, the comparison key is not compared using equals, but "=", as long as the address is not equal (that is, not the same object) can coexist, that is, the key can be repeated.
LinkedHashMap maintains a two-way circular linked list on the basis of HashMap. An important parameter is accessOrder. When accessOrder is set to true, the recently accessed object will be moved to the header each time the get method is called, and the object will be removed when the capacity is exceeded, it starts from the end and overwrites the boolean removeEldestEntry method to implement an LRU queue.
WeakHashMap, but the key is referenced by weak. It is automatically cleared when it is not used. Resizing policy: tab. length * 2. In principle: Entry <K, V> extends WeakReference <K> implements Map. entry <K, V>, so entry is an implementation class with weak references. The key method is expungeStaleEntries, which is called during various map operations, this method also relies on listening to the ReferenceQueue queue status of the key to determine whether there is really no object reference.
Set
Boolean contains (Object o );
Boolean add (E e );
Boolean remove (Object o );
SortedSet, the interface method is similar to SortedMap:
SortedSet <E> subSet (E fromElement, E toElement );
SortedSet <E> headSet (E toElement );
SortedSet <E> tailSet (E fromElement );
E first ();
E last ();
Correspondingly, NavigableSet is similar to NavigableMap, and the methods are not listed.
TreeSet is similar to TreeMap. In fact, the internal implementation is a TreeMap.
HashSet. Note that there are two implementations. When there are less than three constructor parameters, HashMap is used internally. Otherwise, LinkedHashMap is used.
RegularEnumSet and JumboEnumSet, which are common enumeration sets (displacement is used to indicate the possibility of various combinations to minimize space usage, and a maximum of 64 enumeration values cannot be exceeded ), the latter is suitable for a large number of enumeration sets (using object arrays honestly ).
LinkedHashSet is actually a thing with LinkedHashMap.
BitSet is called set but does not implement the set interface. Use bits to store the existence of a certain number. For example, if only one long or 64-bit number exists, it can store 0 ~ 63. The actual internal data type is long [].
Void flip (int bitIndex );
Void flip (int fromIndex, int toIndex );
Void set (int bitIndex );
Void set (int fromIndex, int toIndex, boolean value );
Void clear (int bitIndex );
Int length ();
Int size ();
The size method returns the number of actually used bits; the length method returns the length in a logical sense (for example, the maximum value in the number is 80, then add 0, the logical length is 81 ).
Expansion Policy: Math. max (2 * words. length, wordsRequired ).
Dictionary
Enumeration <K> keys ();
Enumeration <V> elements ();
V get (Object key );
V put (K key, V value );
V remove (Object key );
It has been deprecated. Map is used to implement the same function.
The last figure is from this website. It is very helpful to grasp these container types from a macro perspective: