Use a hash in Java to query stock quotations
China Construction Bank Yunnan Baoshan Branch
Yang shaofang
---- In java, a hash table class Hashtable is provided. With this class, we can store data in a specific way to achieve quick retrieval. This article describes how to use java hash to query the closing data of a stock.
I. Principles of hash
---- Hash table is an important storage and retrieval method for linear tables. You can quickly search nodes in the scattered list. The basic idea of the hash algorithm is that the storage address of the node is determined by the key code value of the node, that is, the key code value k is the independent variable, through a certain function relationship h (called a hash function), calculate the corresponding function value h (k) and interpret this value as the storage address of the node, store the node to this address. During retrieval, the key code value to be retrieved is calculated using the same hash function. Then, go to the corresponding address to obtain the node data you want to find. Therefore, the length of an average search does not depend on the number of elements in the table.
---- Load factor is the most important indicator of the hash list, that is, the ratio of the number of nodes in the hash list to the number of summarized points in the table. It describes the saturation of the hash list, the closer the load factor is to 1.0, the higher the memory usage efficiency, the longer the element searching time. Similarly, the closer the load factor is to 0.0, the shorter the element searching time, but the larger the memory waste. The default load factor of the Hashtable class is 0.75.
Ii. Hashtable class
---- The Hashtable class provides a complete hash function, allowing us to easily construct and use a hash to query information.
---- 1. Create a hash object
---- The constructor of the Hashtable class mainly has the following forms:
Public Hashtable (int initialCapacity, float loadFactor );
Public Hashtable (int initialCapacity );
Public Hashtable ();
In this example, we use the simplest one:
Hashtable stockInfo = new Hashtable ();
---- 2. Data Filling
---- After constructing a Hashtable object, we can enter the data in the object for future query. The Hashtable class provides the put Method to complete data loading. Its prototype is as follows:
---- Public synchronized Object put (Object key, Object value );
---- 3. query data
---- You can use the get method to query data. Its prototype is as follows:
---- Public synchronized Object get (Object key)
---- 4. Other common methods
Public int size ();
// Return the number of nodes in the hash table.
Public boolean isEmpty ();
// Determine whether the hash is empty
Public boolean containsValue (Object value );
// Determine whether the hash list contains a value