Java hash table, corresponding container class tree, hashmap how to resolve conflicts

Hashtable:
Hashmap, Hashtable, concurrentHashMap, HashSet
tree:
TreeMap, TreeSet
CopyOnWriteArrayList is a thread-safe variant of ArrayList, wherein all the variable operation (add, set, etc.) of the underlying array is through a fresh copy of achieved.

treeset inherited from the treemap, hashset inherited from hashmap

Entry: a combination of key and value.

For treemap, since it uses a bottom "red-black tree" to save a collection of Entry, which means TreeMap add elements, remove elements of performance than HashMap low.
When the additive element TreeMap necessary to find the insertion position through the cycle of new Entry, thus comparison resistance performance;
when removed treemap elements, to find the right by traversing Entry you can also compare the performance of consumption.
But treemap, treeset, hashset has the advantage over hashmap:
All Entry TreeMap is always kept orderly by key state in accordance with the rules specified order, treeset all elements always remain orderly state based on the specified collation

HashMap call hashCode () method to calculate hashCode. Because in Java, two different objects could have the same hashCode, so that different keys may have the same hashCode, it will lead to conflict.

bucket: This array can store the position of the element is called "bucket (bucket)", each bucket has its designated index, the elements of the bucket system can be stored in its index based on quick access.

Hashmap inside the bucket appeared in the form of a single linked list, hash table to solve a problem is the conflict in the hash value, typically two methods: Method list and open addressing method. Method object list is organized into the same hash value in a hash chain values ​​corresponding slot; open groove address method is through a detection algorithm, when a case where the continue searching has occupied a slot that can be used bit.

Two kinds:

Open Addressing method: When a collision occurs, use of a probe (also known as probe) technique for forming a probe in the hash table (measurement) sequence. This sequence along find cell by cell until it finds a given keyword or across an open address (i.e., the location is empty) until the (To insert, in detecting the open address can be inserted to be deposited in the new node address unit). Probe to find an address when opening the table indicates no keywords of unknown origin, that lookup fails.

Chain address method: all keywords link nodes synonyms in the same single list. If the selected hash table length m, the hash table can be defined as a pointer to an array of T m by the head pointer consisting of [0 ... m-1]. Any hash address of node i, are inserted in T [i] as a single linked list head pointer. T is the initial value of each component should be a null pointer.

Reprinted: https: //my.oschina.net/134596/blog/668416

Published 53 original articles · won praise 5 · Views 427

Guess you like

Origin blog.csdn.net/qq_45287265/article/details/105013831