Detailed explanation of the Map interface of Java collections

Overview of Java collections

As can be seen from the figure, in addition to Java, Mapother than the end of the class, other classes implement the Collectioninterface. And, at Mapthe end of the classes implement the Mapinterface.

Insert picture description here
 
 

Summary of the underlying data structure of Java collections

1. List

The stored elements are ordered and repeatable.

  • Arraylist: Object[]Array
  • Vector: Object[]Array
  • LinkedList: 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 on HashMapimplementation, are based HashMapto preserve elements
  • LinkedHashSet: LinkedHashSetIt is the HashSetsubclass, and it is through the inner LinkedHashMapachieved. Somewhat similar to LinkedHashMapits interior is based on the HashMaprealization of the same
  • TreeSet: Ordered, unique, red-black tree

3. Map

  • HashMap: Before JDK1.8, it is HashMapcomposed of array + linked list. The array is HashMapthe 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 time
  • LinkedHashMap: LinkedHashMapInheriting self HashMap, so its bottom layer is still based on array and linked list/red tree. In addition, LinkedHashMapon 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 is Hashtablesubject, the list is mainly to resolve hash collision exists
  • TreeMap: Red-Black Tree

 
 

The difference between HashMap and Hashtable

1. Is the thread safe?

HashMap It is not thread-safe.

HashTableIt is thread-safe, because the HashTablemethods of the basic internal have been synchronizedmodified.

2. Efficiency

Because the thread safety problems HashMapthan HashTablea little high efficiency. In addition, it is HashTablebasically 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.

HashTableNull 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

HashMapThe 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 Hashtableis no such mechanism.
 
 

The difference between HashMap and HashSet

HashSetIt is based on the underlying HashMapimplementation. HashSetThe source code is very, very little, because in addition clone(), writeObject(), readObject()is HashSetoutside of themselves having to implement, other methods are called directly HashMapmethod.

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

ConcurrentHashMapAnd Hashtablethe difference is mainly reflected in the different ways to achieve thread-safe.

1. The underlying data structure

JDK1.7 the ConcurrentHashMapunderlying array + piecewise linked list implementation, the data structure used with JDK1.8 HashMapstructure 1.8, is an array list + / red and black binary tree.

HashtableAnd before the JDK1.8 HashMapsimilar underlying data structures are employed in the form of an array + list.

2. A way to achieve thread safety

JDK1.7 ConcurrentHashMapachieves 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.

Insert picture description here

JDK1.8 to ConcurrentHashMapabandon the concept of Segment, but directly linked list data structure Node array + + red-black tree implemented using concurrency control synchronizedand operate CAS. synchronizedOnly lock the current chain or the first node red-black tree, the whole looks like a thread-safe and optimized HashMap
Insert picture description here
Hashtableto achieve thread safety by full table lock, which is equivalent to the entire hash table only had a lock, get/ putall 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 putadded element, nor can it be used get, and the performance will be very poor in a concurrent scenario.

Insert picture description here
 
 

Supplement: The difference between Arraylist and LinkedList

1. Whether to ensure thread safety

ArrayListAnd LinkedListare not synchronized, that is not guaranteed to be thread safe;

But it Vectoris thread-safe.

2. The underlying data structure

ArraylistUsing a bottom Objectarray
LinkedListunderlayer 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

ArrayListArray 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, ArrayListthe 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.

LinkedListLinked 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

ArrayListSupport 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 LinkedListdoes not support efficient random element access.

5. Memory footprint

ArrayListWaste of space is mainly reflected in the list at the end of the list will reserve some space capacity
LinkedListof the space cost is reflected in each of its elements we need to consume more than ArrayListmore 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

Guess you like

Origin blog.csdn.net/weixin_43901865/article/details/112718258