Query for stock quotes using hash table in Java
CCB Yunnan Province Baoshan Area Branch
Yang Shaofang
----in Java, a hash-class Hashtable is provided, using which we can store data in a specific way to achieve fast retrieval. This article takes a look at the closing data for the stock as an example to explain in detail how to use the hash list in Java.
First, the principle of the hash table
----Hash list, also known as a hash table, is an important storage mode and retrieval method in linear tables. In a hash table, you can quickly retrieve a node. The basic idea of the hash-list algorithm is that the key code value of the node determines the storage address of the node, that is, the key code value k is the independent variable, through a certain function relation H (called the hash function), the corresponding function value h (k) is calculated, and the value is interpreted as the storage address of the node, and the node is stored in the address, According to the key value to retrieve, the same hash function is used to compute the address, and then to the corresponding address to obtain the node data to find. Therefore, a hash table has an important feature: the average retrieval length does not depend directly on the number of elements in the tables.
One of the most important indicators of----hash list is the load factor, which is the ratio of the number of nodes in the hash table to the sum of the sums that can be accommodated in the list, which describes the saturation of the hash list. The closer the load factor is 1.0, the higher the efficiency of the memory, the longer the search time of the element, the more the load factor is close to 0.0, the less the search time of the element, but the greater the waste of the memory. The default load factor for the Hashtable class is 0.75.
Second, Hashtable class
----Hashtable class provides us with a complete list of features that allow us to easily construct and use a hash table to query for information.
----1. Create a hash Table object
The constructors of the----Hashtable class are mainly in the following ways:
Public Hashtable (int initialcapacity, float loadfactor);
Public Hashtable (int initialcapacity);
public Hashtable ();
In the example of this article, we use the simplest one:
Hashtable stockinfo = new Hashtable ();
----2. Filling data
----When the Hashtable object is constructed, we can populate the object with data for later queries. The Hashtable class provides a put method for filling data, and its prototype is as follows:
----Public synchronized object (object key, object value);
----3. Query data
----query data can use the Get method, which has the following prototype:
----Public synchronized object get (object key)
----4. Other commonly used methods
public int size ();
Returns the number of nodes in a hash table
public Boolean isempty ();
To determine whether a hash table is empty
public boolean Containsvalue (Object value);
To determine if a value is contained in a hash table
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.