Java hash table class hashtable

Source: Internet
Author: User

Original address: http://www.javaresearch.org/article/showarticle.jsp? Column = 545 & Thread = 17460

Hash table is an important storage method and a common retrieval method. The basic idea is to take the value of the link code as the independent variable, calculate the corresponding function value through a certain functional relationship, and interpret this value as the storage address of the node, store the node to the storage unit corresponding to the storage address. The key code retrieval method is used for retrieval. Now, a hash table has a complete set of algorithms for insertion, deletion, and conflict resolution. In Java, hash tables are used to store objects for fast retrieval.

Java. util. hashtable provides a way for users to use a hash table without considering how the hash table actually works.
The hash table class provides three constructor methods:
Public hashtable ()
Public hashtable (INT initialcapacity)
Public hashtable (INT initialcapacity, float loadfactor)
The initialcapacity parameter is the initial capacity of hashtable. Its value must be greater than 0. Loadfactor, also known as the load factor, is a float floating point number between 0.0 and 0.1. It is a percentage that indicates when the hash table needs to be expanded. For example, there is a hash table with a capacity of 100 and a load factor of 0.9, when 90% of the hash table capacity is used, the hash table is automatically expanded into a larger hash table. If you do not assign these parameters, the system automatically processes them without worrying about them.
Hashtable provides basic insertion and retrieval methods.
■ Insert
Public synchronized void put (Object key, object value)
Set a key keyword for the object Value and add it to hashtable. If this keyword already exists, the old object corresponding to this keyword is updated to the new object value. This indicates that the same keywords in the hash table cannot correspond to different objects (from the basic idea of the hash table, this is also obvious ).
■ Search
Public synchronized object get (Object key)
Obtain the corresponding object based on the given keyword key.
Public synchronized Boolean containskey (Object key)
Determines whether the hash table contains the key keyword.
Public synchronized Boolean contains (object value)
Determine whether the value is an element in the hash table.
■ Delete
Public synchronized object remove (Object key)
Delete the object corresponding to the key keyword from the hash table.
Public synchronized void clear ()
Clear hash table
In addition, hashtalbe provides methods to obtain the corresponding enumeration set:
Public synchronized enumeration keys ()
Returns the enumerated object corresponding to the keyword.
Public synchronized enumeration elements ()
Returns the enumerated object corresponding to the element.
Example 8.5 hashtable. Java provides an example of using hashtable.
Example 8.5 hashtalbe. java.
// Import java. Lang .*;
Import java. util. hashtable;
Import java. util. enumeration;
Public class hashapp {
Public static void main (string ARGs []) {
Hashtable hash = new hashtable (2, (float) 0.8 );
// Create an object hash for a hash table. The initial capacity is 2 and the load factor is 0.8.

Hash. Put ("Jiangsu", "Nanjing ");
// Specify the keyword "Nanjing" for the string object "Jiangsu" and add it to the hash
Hash. Put ("Beijing", "Beijing ");
Hash. Put ("Zhejiang", "Hangzhou ");

System. Out. println ("The hashtable hash1 is:" + hash );
System. Out. println ("the size of this hash table is" + hash. Size ());
// Print the hash content and size

Enumeration enum1 = hash. Elements ();
System. Out. Print ("the element of hash is :");
While (enum1.hasmoreelements ())
System. Out. Print (enum1.nextelement () + "");
System. Out. println ();
// Print the content in the hash in sequence
If (hash. containskey ("Jiangsu "))
System. Out. println ("The capatial of Jiangsu is" + hash. Get ("Jiangsu "));
Hash. Remove ("Beijing ");
// Delete the object corresponding to the keyword Beijing
System. Out. println ("The hashtable hash2 is:" + hash );
System. Out. println ("the size of this hash table is" + hash. Size ());
}
}

Running result:
The hashtable hash1 is: {Beijing = Beijing, Zhejiang = Hangzhou, Jiangsu = Nanjing}
The size of this hash table is 3
The element of hash is: Beijing Hangzhou Nanjing
The capatial of Jiangsu is Nanjing
The hashtable hash2 is: {Zhejiang = Hangzhou, Jiangsu = Nanjing}
The size of this hash table is 2

Hashtable is a subclass of the dictionary class. In the dictionary class, the keyword is mapped to the data value. A dictionary class is an abstract class. There is also a class properties in Java. util, which is a subclass of hashtable. You can use it to perform operations related to object attributes.

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.