WeakHashMap VS HashMap

WeakHashMap VS HashMap

The key in WeakHashMap adopts the method of "weak reference". As long as the key in WeakHashMap is no longer referenced externally, the corresponding key-value pair can be recycled by the garbage collector. The key in the HashMap adopts the "strong reference" method. When the key is no longer referenced by the outside world, it can be recycled by the garbage collector only after the key is deleted from the HashMap.

Difference between HashMap and TreeMap

1. Differences in implementation:
HashMap: based on the hash table implementation. TreeMap: Based on red-black tree implementation.
2.TreeMap can sort the records it saves according to the key.
3.HashMap: suitable for inserting, deleting and finding elements in the Map.
Treemap: suitable for traversing keys in natural or custom order.
HashMap is usually a bit faster than TreeMap.

LinkedHashMap: The HashMap collection can be guaranteed to be ordered. The order of deposit is the same as the order of withdrawal

Implementation principle of Hashset

For HashSet, it is implemented based on HashMap. The bottom layer of HashSet uses HashMap to save all elements, so the implementation of HashSet is relatively simple. The operations related to HashSet are basically completed by directly calling the related methods of the underlying HashMap. The elements in the HashSet are stored on the key of the HashMap , and the values ​​in the value are a unified private static final Object PRESENT = new Object();

TreeSet

Thread-unsafe, the elements in the Set collection can be sorted. The underlying data structure of TreeSet is a binary tree.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325511892&siteId=291194637