Learn HashSet and HashTable from JDK source code

HashSet

There are three types of collections in Java, one is List, the other is Queue, and the other is Set. The elements in the first two sets are ordered, and the elements can be repeated; the elements in the last set are unordered, but the elements are not repeatable.

Set:

1. Used to store unordered (the order of deposit and retrieval is not necessarily the same) elements, the value can not be repeated

2. The essence of object equality is the object hashCode value (java is calculated based on the memory address of the object, the hashcode of different objects is not necessarily different), if you want two different objects to be considered equal , You must override Object's hashCode method and equals method. For example, the string class rewrites the hashcode method. The calculated hashcode value is not the actual memory address of the object, and equals is also rewritten.

String.hashcode()

 String.equals

1. First verify whether it is the same object

2. Verify whether it is the same type (String), and then verify that the values ​​are equal

Hashset also supports serialization, shallow copy

Hashset is still Hashset internally, but it is no longer a key-value pair that is directly added by calling its add

Look at its add method:

Directly call map.put to put the key represented by e and present member variables

The map here is the structure of the value stored in the hashset. You can see that the key is put, and the value corresponding to the key is an instance of object

 

Since its hashmap is used, its construction method is actually to define hashmap, so it is the four construction methods of hashmap

Then the value is not as convenient as hashmap. You can directly take the value corresponding to a key. Taking the value in the hashset is to obtain an iterator, get all the keys of the internal hashmap, and then traverse the operation.

 So the internal storage structure is the same as the hashmap structure, and the hashset is also non-thread safe

The difference between ArrayList and HashSet

1. The former is ordered and can store duplicate values, the latter is unordered and cannot store duplicate values, because the hashmap key cannot be repeated

2. Arraylist is filled and expanded by 1.5 times, Hashset expansion mechanism is the same as hashmap

HashTable

The map interface implemented by HashTable supports serialization and shallow copy

Hashtable is also a hash table implemented by the "zipper method" (just an array plus a single linked list). Its internal storage structure is an entry array. Similar to hashmap, it also has a load factor and initial capacity.

There are also 4 kinds of construction methods

The first one supports the initial specified capacity and load factor as follows. At this time, the entry will be allocated memory space, and the initialization threshold is the initial capacity and the smaller of (2 times 31 times-1) -8 (maximum number of bytes) +1 value

 

The second type only specifies the initialization size

 

The third uses the default initial capacity and load factor, the initial capacity is 11

 

 

The fourth type is to directly put a map into it to initialize a hashtable. At this time, the hashtable capacity will become 2 times the number of key-value pairs of the map and the larger value of the default capacity, and then map put into a

 

 

The hashmap here is different from the hashtable. The number of keys / load factor +0.75 of the map is used during initialization, and the calculated value is compared with the 30th power of 2, and the smaller of the two and the threshold are used. Compare and assign a threshold value to the power of 2 that is greater than the calculated value to the nearest, which is convenient for subsequent resize expansion, and then the values ​​in the map are sequentially put through the round-robin.

 

Comparison of HashTable and HashMap

1. HashTable is based on the Dictionary class, and HashMap is based on AbstractMap. Dictionary is an abstract parent class of any class that can map keys to corresponding values. AbstractMap is an implementation based on the Map interface, but both hashtable and hashmap implement the Map interface

2. Hashmap can put the value of both the key and the value are null, but you can only put one of these values, so the hashmap to determine whether there is a key to use containskey (key must be unique), but can not use get , So the value corresponding to multiple keys is null, and the key and value of the hashtable cannot be null, otherwise a null pointer error will be reported

Hashmap processing:

Therefore, the hashmap takes into account such a case where the key is null, so that its hash is calculated as 0, and the key that is not null then calls the object's hashcode method to calculate the hash

The get method of hashmap is as shown in the following figure. If it does not exist, it may return null or the value of the key is null, which cannot be determined.

Hashtable processing:

The design of hashtable does not consider so much, but directly calls the hashcode of its key, then null.hashcode will definitely report an error

 

Hashtable will detect whether the value corresponding to the put key is null

 

 

3. Hashmap is not thread-safe by default, and hashtable thinks that the basic public methods are all decorated with synchronized, so it is synchronized

4. The two expansion methods are different. Hashmap expansion is the resize method, the capacity becomes old * 2, and hashtable is the rehash method, the capacity becomes old * 2 + 1,

5. The two internal traversal implementations are different:

Hashmap key value traversal as iterator

 

The hashtable key value traversal is Enumerator

 

 6. The method for obtaining the position of the key is different:

In the hashmap, the logic operation is first replaced by AND logic to speed up, the nth power of 2 -1 bit all 1 binary bit and then the hash of the key and the position of the key value pair are calculated, and the hash value is not a simple hashcode, and The high 16 bits of the hashcode of the key are used for XOR operation

 

 

 

 Hashtable calculates a hashcode directly according to the key (may be a negative value), and then calculates the positive value with the 31th power of 2 -1 and then modulo the length of the current hash table, and then determines the position of the key value pair, then The efficiency of the modulus is definitely not higher than the operating efficiency of the logic

 

reference

https://blog.csdn.net/fujiakai/article/details/51585767 The  difference between hashmap and hashtable

https://wiki.jikexueyuan.com/project/java-collection/hashtable.html  hashmap implementation principle

Guess you like

Origin www.cnblogs.com/tr1ple/p/12701149.html