In-depth exploration of the implementation principle of j2-hashtable (1)

Source: Internet
Author: User

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.

 
 
  1. PublicHashtable (intinitialCapacity); // specify the capacity
  2. PublicHashtable (){
  3. This (11); // The default capacity is 11. Why 11 instead of 10?
  4. }

Demo1

 
 
  1. HashtablesTable=newHashtable();  
  2. sTable.put("wuhua","wuhua");  
  3. sTable.remove("wuhua");  
  4. 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.

 
 
  1. privatetransientHashtableEntrytable[];  
  2. 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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.