Hashtable is the implementation of the data structure of key values in Java. You can use the key to store and retrieve the value, which is The identifier that stores the value . Obviously the "key" should be unique.
Java.util.Hashtable expands dictionary and implements map. An object with a non- null value can be used as a key or a value. The Hashtable key must implement the Hashcode () and Equals () methods. at the end of this article, you will find the reason behind this condition.
In general, use the empty constructor Hashtable () to create a Hashtable in Java . This is a bad decision, and often repeated mistakes. Hashtable has two other constructors
Hashtable (int initialcapacity)()
And
Hashtable (int initialcapacity,float loadfactor)(,)
. The initial capacity is the number of buckets created when the Hashtable is instantiated. a hash table stored in the logical space of the bucket .
hashes and Hash tables
before you look at Java's Hashtable, you should understand the general hash. Assuming that V is the value to store, and K is the key to store/retrieve, H is the hash function that stores v at H (k) of the table. to get the value, calculate h (k) so that you can get the position of V directly. Therefore, in a key-value pair table, you do not need a sequential scan key to identify the value.
h (k) is a hash function, which is used to find the location where the corresponding value is stored v. H (k) cannot calculate to indeterminate space. the storage assigned to Hashtable is restricted in the program. Therefore, the hash function h (k) should return A number within the allocated spectrum (the logical address space) .
hashing in Java
Java hashes are computed using the Hashcode () method from the key and value objects. the following is the core code from Hashtable, which calculates the hashcode ' H '. You can see that the hashcode () method of key and value is called.
H + = E.key.hashcode () ^ E.value.hashcode () ; + = E.key.hashcode () ^ E.value.hashcode ();
It is a good idea to use the Hashcode () method in your custom object. String has its own Hashcode method, which calculates the hashcode value as follows:
< Span class= "lit" > < Span class= "pun" >< Span class= "pun" >< Span class= "lit" >
s[0]*31^ (n-1) + s[1]*31^ (n-2) + ...
+ ... + s[n-1]
If there is no Hashcode () method, it is derived from the object class. the following is a Javadoc comment from the hashcode () method of the object class :
returns the hash code value of the object. This approach is supported for the benefit of hash tables, such as those provided by Java.util.Hashtable.
If you are writing a custom hashcode (), then follow these contracts:
The general contract for Hashcode is that whenever the object is called on the same object multiple times while the Java application is executing, the Hashcode method must always return the same integer, provided that no information is modified in the Equals comparison on the object.
The following are the performance improvements for Hashtable.
If two objects are equal according to the Equals (object) method, calling the Hashcode method on each object in two objects must produce the same integer result.
Hashcode () guarantees different integers by using the object's internal address.
Collision in Hashtable
conflicts can occur when we try to limit the output of the hash function within the range of the allocated address range limits. for two different keys K1 and K2, if we have h (k1) = h (K2), then this is the collision in the hash table. What this means is that our hash function indicates that we store two different values in the same location (the keys are also different).
when colliding, there are several ways to solve it. name several hash table conflict resolution techniques, "single link", "Open Addressing", "Robingerhach", "cuckoo hash" and so on. The hash table for Java uses a "detach link" to resolve conflicts in Hashtable.
Crash Solutions in Java Hashtable
Java uses a separate link for conflict resolution. recall that Hashtable stores elements in a bucket. in a separate link, each bucket stores a reference to the linked list. Now suppose you have stored an element in bucket 1. This means that in the 1th bucket, you will have a list of references to the list, in which you will have two cells. in these two cells, you will have the key and its corresponding value.
Why do I need to store my keys? Because when there is a conflict, when two keys produce the same hash code and point to the same bucket (assuming bucket 1), you also store the second element in the same bucket. Add this second element to the list of links you have created as adjacent elements.
now, when you retrieve a value, it computes the hash code and directs it to a bucket with two elements. You can scan these two elements sequentially and use their equals () method to compare keys. when key math you get the corresponding value. Hopefully you have a reason behind the condition that your object must have the Hashcode () and Equals () methods.
Java has a private static class entry in Hashtable. It is a list of implementations, and you can see that it stores the key and value.
Hash Show
To get better performance from your Java hashtable, you need to
Using the initialcapacity and Loadfactor parameters
2)
when instantiating a hashtable use them wisely .
Initialcapacitiy is the number of buckets created when the Hashtable is instantiated. the number of buckets and the probability of collisions are reversed. If you need more buckets, the likelihood of collisions is small.
For example, if you want to store 10 elements, and if you want to set initialcapacity to 100, you will have 100 buckets. you will calculate Hashcoe () only 10 times, with a spectrum of 100 barrels. there is very little likelihood of collisions.
However, if you want to provide Hashtable's initialcapacity as 10, the likelihood of collisions is very high. Loadfactor decide when to automatically increase the size of the Hashtable. The default size for Initialcapacity is 11,loadfactor. 75 If the size of the Hashtable is 3/4, the size of the Hashtable will increase.
The new capacity in Java Hashtable is calculated as follows:
int newcapacity = oldcapacity * 2 + 1; ;
If you provide a smaller capacity and load factor, and you typically perform rehash (), this can cause performance issues. Therefore, in order to achieve the efficient performance of Hashtable in Java, when instantiated, the initialcapacity is the 25%,loadfactor you need to be 0.75.
Java's Hashtable