Hashtable instances have two parameters that affect their efficiency: capacity and load factor. The load factor is always 75% in CLDC implementation, and this value can be specified in other versions ). When the number of objects in Hashtable exceeds the load factor and a combination value of the current capacity, this may be obtained through the corresponding algorithm), the rehash method is called to Increase the capacity.
Implementation principle of j2-hashtable
First, let's take a look at the usage of Hashtable.
Hashtable has two constructors.
- PublicHashtable (intinitialCapacity); // specify the capacity
- PublicHashtable (){
- This (11); // The default capacity is 11. Why 11 instead of 10?
- }
Demo1
- HashtablesTable=newHashtable();
- sTable.put("wuhua","wuhua");
- sTable.remove("wuhua");
- sTable.clear();
The above is a simple usage.
Hashtable source code explanation
Before learning about the source code, let's take a look at some keywords that are not commonly used in java.
Transient
When an object is serialized, if a variable of the object is transient, the variable will not be serialized. That is to say, if the member variable of a class is transient, the value of the transient variable will not be saved when an instance of this class is saved to the disk through ObjectOutputStream. Because when this object is read from the disk, the variable of the object is not assigned a value. This article also mentions that when reading an instance of a class from a disk, the constructor of this class is not actually executed, but the status of the instance of this class is read, and pay this status to the object of this class.
The Transient keyword is very important. It can be seen that this keyword is used in the source code.
- privatetransientHashtableEntrytable[];
- privatetransientintcount;
Only the above two fields are defined in the source code, but these two fields are used to store style = "COLOR: #000000 "href =" http://storage.it168.com/"target = _ blank> stores the container field of Hashtable, so it can be said that Hashtable cannot be serialized.