table of Contents
Overview of Java collections
As can be seen from the figure, in addition to Java, Map
other than the end of the class, other classes implement the Collection
interface. And, at Map
the end of the classes implement the Map
interface.
Summary of the underlying data structure of Java collections
1. List
The stored elements are ordered and repeatable.
Arraylist
:Object[]
ArrayVector
:Object[]
ArrayLinkedList
: Doubly linked list (circular linked list before JDK1.6, JDK1.7 cancels the cycle)
2. Set
The stored elements are unordered and non-repeatable.
HashSet
: Disorderly, the only, based onHashMap
implementation, are basedHashMap
to preserve elementsLinkedHashSet
:LinkedHashSet
It is theHashSet
subclass, and it is through the innerLinkedHashMap
achieved. Somewhat similar toLinkedHashMap
its interior is based on theHashMap
realization of the sameTreeSet
: Ordered, unique, red-black tree
3. Map
HashMap
: Before JDK1.8, it isHashMap
composed of array + linked list. The array isHashMap
the main body, and the linked list is mainly to solve the existence of hash conflicts. After JDK1.8, there have been changes in the resolution of hash conflicts. When the length of the linked list is larger than the threshold (the default is 8), the linked list is converted into a red tree to reduce the search timeLinkedHashMap
:LinkedHashMap
Inheriting selfHashMap
, so its bottom layer is still based on array and linked list/red tree. In addition,LinkedHashMap
on the basis of the above structure, a doubly linked list is added so that the above structure can maintain the insertion order of key-value pairs. At the same time, through corresponding operations on the linked list, the access sequence related logic is realized.Hashtable
: + List consisting of an array, the array isHashtable
subject, the list is mainly to resolve hash collision existsTreeMap
: Red-Black Tree
The difference between HashMap and Hashtable
1. Is the thread safe?
HashMap
It is not thread-safe.
HashTable
It is thread-safe, because the HashTable
methods of the basic internal have been synchronized
modified.
2. Efficiency
Because the thread safety problems HashMap
than HashTable
a little high efficiency. In addition, it is HashTable
basically eliminated, do not use it in the code.
3. Support for Null key and Null value
HashMap
You can store null key and value, but there can only be one null as the key, and multiple null as the value.
HashTable
Null keys and null values are not allowed, otherwise it will be thrown NullPointerException
.
4. Initial capacity
If you do not specify the initial value of the capacity when creating
HashMap
The default initialization size is 16. After each expansion, the capacity is doubled.
Hashtable
The default initial size is 11, after each expansion, the capacity becomes the original 2n+1.
5. Expand the capacity each time
If the initial value of capacity is given when creating
HashMap
Will expand it to a power of 2 size
Hashtable
Will directly use the size you give
6. The underlying data structure
After JDK1.8 HashMap
, there have been changes in resolving hash conflicts. When the length of the linked list is greater than the threshold (the default is 8), the linked list is converted into a red-black tree to reduce the search time.
Add why the default value is 8 : At 8, the average search length of the red-black tree is log(N)=3, and the average search length of the linked list is 8/2=4.
There Hashtable
is no such mechanism.
The difference between HashMap and HashSet
HashSet
It is based on the underlying HashMap
implementation. HashSet
The source code is very, very little, because in addition clone()
, writeObject()
, readObject()
is HashSet
outside of themselves having to implement, other methods are called directly HashMap
method.
HashMap | HashSet |
---|---|
Implemented the Map interface | Implement the Set interface |
Store key-value pairs | Store objects only |
Call put() to add elements to the map | Call the add() method to add elements to the Set |
HashMap uses Key to calculate hashcode | HashSet uses member objects to calculate the hashcode value. For two objects, the hashcode may be the same, so the equals() method is used to determine the equality of objects |
The difference between ConcurrentHashMap and Hashtable
ConcurrentHashMap
And Hashtable
the difference is mainly reflected in the different ways to achieve thread-safe.
1. The underlying data structure
JDK1.7 the ConcurrentHashMap
underlying array + piecewise linked list implementation, the data structure used with JDK1.8 HashMap
structure 1.8, is an array list + / red and black binary tree.
Hashtable
And before the JDK1.8 HashMap
similar underlying data structures are employed in the form of an array + list.
2. A way to achieve thread safety
JDK1.7 ConcurrentHashMap
achieves thread safety through segmented locks. First, the entire bucket array is segmented. Each lock only locks part of the data in the container. There will be no lock competition when multi-threaded access to different data segments in the container. Improve concurrent access rate.
JDK1.8 to ConcurrentHashMap
abandon the concept of Segment, but directly linked list data structure Node array + + red-black tree implemented using concurrency control synchronized
and operate CAS. synchronized
Only lock the current chain or the first node red-black tree, the whole looks like a thread-safe and optimized HashMap
Hashtable
to achieve thread safety by full table lock, which is equivalent to the entire hash table only had a lock, get
/ put
all The operation is all synchronized
. In multithreaded access, when one thread accesses or manipulates the object, other threads can only enter the blocking or polling state. For example put
, if a thread uses the added element, other threads cannot use the put
added element, nor can it be used get
, and the performance will be very poor in a concurrent scenario.
Supplement: The difference between Arraylist and LinkedList
1. Whether to ensure thread safety
ArrayList
And LinkedList
are not synchronized, that is not guaranteed to be thread safe;
But it Vector
is thread-safe.
2. The underlying data structure
Arraylist
Using a bottom Object
array
LinkedList
underlayer using a doubly linked list data structure (as before JDK1.6 circular linked list, JDK1.7 canceled cycle).
3. Are insertions and deletions affected by element position
ArrayList
Array storage is used, so the time complexity of inserting and deleting elements is affected by the position of the element. For example add(E e)
, when executing a method, ArrayList
the specified element will be appended to the end of the list by default. In this case, the time complexity is O(1). But if you want to insert and delete elements at the specified position i ( add(int index, E element)
), the time complexity is O(ni). Because in the above operation, the i-th and (ni) elements after the i-th element in the set must be moved backward/forward by one bit.
LinkedList
Linked list storage is used, so for add(E e)
method insertion, the time complexity of deleting elements is not affected by the position of the element, which is approximately O(1). If you want to insert and delete elements at the specified position i ( (add(int index, E element)
) The time complexity is approximately o(n )) Because you need to move to the specified position before inserting.
4. Whether to support fast random access
ArrayList
Support fast random access. Fast random access is to quickly obtain the element object (corresponding to the get(int index)
method) through the sequence number of the element .
It LinkedList
does not support efficient random element access.
5. Memory footprint
ArrayList
Waste of space is mainly reflected in the list at the end of the list will reserve some space capacity
LinkedList
of the space cost is reflected in each of its elements we need to consume more than ArrayList
more space (due to the direct deposit and direct predecessor and successor data).
Reference:
https://github.com/Snailclimb/JavaGuide
https://www.cnblogs.com/chengxiao/p/6842045.html