Java uses the hash table to realize the stock market

xiaoxiao2021-03-06  107

---- In Java, a hash table HashTable, which uses this class, we can store data in a specific way, thereby achieving fast retrieval. This article uses the collection data of the stock as an example to explain the method of use of a list of java in Java.

First, the principle of the hash table

---- The hash table, also known as a hash table, is an important storage method and retrieval method in linear forms. In the hash table, the node can be quickly retrieved. The basic idea of ​​the hash table algorithm is: determine the storage address of the node's key code value, that is, the key code value k is the independent variable, and calculates the corresponding corresponding function. Function value h (k), interpret this value as the storage address of the node, store the node into the address, and then calculate the address with the same hash function according to the key code value to retrieve, and then To get the node data you want to find in the corresponding address. Therefore, there is an important feature of the hashing table: the length of the average retrieval does not directly depend on the number of elements in the table.

---- The most important indicator of the hash table is the load factor, that is, the ratio of the number of points in the hash table and the summary points that can accommodate in the table, which describes the saturation of the hash table, the closer the load factor, the memory The higher the use efficiency, the longer the search time of the element, the more the load factor is closer to 0.0, the shorter the search time of the element, the greater the waste of memory. HashTable class default load factors are 0.75.

Second, HashTable class

---- HashTable class provides us with a complete function of the hash table, allowing us to make it easy to construct and use the hash table and query information.

---- 1. Create a list object

---- The constructor of the HashTable class mainly has several following form:

Public HashTable (int initialcapactor); public hashtable (int initialcapacity); public hashtable (); in this document, we use the simplest: Hashtable stockInfo = new hashtable ();

---- 2. Filling data

---- After constructing the HashTable object, we can fill in the object in this object so that you will query later. The HashTable class provides a PUT method to complete the loading of the data, its prototype is as follows:

---- Public Synchronized Object Put (Object Key, Object Value);

---- 3. Query data

---- Query data can use the GET method, whose prototype is as follows:

---- Public Synchronized Object Get (Object Key)

---- 4. Other common methods

Public int size (); // Returns the number of nodes in the hash table PUBLIC BOOLEAN ISEMPTY (); / / Decorates whether the loose table is empty public boolean contactsValue (Object value); / / Declats whether there is a value in the lanterns PUBLIC SYNCHRONIZED Boolean Containskey (Object Key); / / DIP in the hash table contains a node public synchronized void clear (); / / Clear the entire hash table

Third, STRINGTOKENIZER Class

---- The main purpose of the StringTokenizer class is to analyze the string as a delimiter, and analyze it as a token (understandable word), and the delimiter can specify itself.

---- The constructor has the following form: Public StringTokenizer (String Str, String Delim, Boolean ReturntoKens); Public StringTokenizer (String Str, String Delim); public StringTokenizer (String Str); where Str is the character to be analyzed Strings, Delim is a delimiter, and tokens describes whether to deliver a delimiter as a token.

---- Other common methods are:

Public Boolean HasmoreToKens (); // Decision String also has tokenpublic string nextToken (); // StringTokenizer object's next token

Fourth, instance

---- The stock market used in this article is the closed market of the Shanghai and Shenzhen Stock Exchange, the file name is hqsj.txt, the following is a line of data in the file:

---- 600122 Hongtu Trumpet 18.90 18.80 18.90 18.20 18.27 3155 582.96

---- The following is a complete source program, using Javac compiles by JAVAC in JDK1.2.

Import java.io. *; import java.util. *; import java.awt. *; import java.applet. *; import java.awt.event. *;

public class StockQuote extends Applet implements ActionListener {private static final File INFO_FILE = new File ( "hqsj.txt"); private Hashtable stockInfo; TextField stockID; Button button1; private String quoteid, quotename;

Public void init () {add ("stock code")); stockid = new textfield (6); add (stockid); button1 = new button ("query); button1.addactionListener (this); add ( Button1); Resize (500, 300);

Public void start () {loadingInfo ();

Protected Boolean LoadInfo () {String Fileline; StringTokenizer Tokenize; String ID; StringBuffer Name

Try {// Create a stream bufferedReader stockInput = new bufferedReader (New fileReader); // Create a HashTable object stockinfo = new hashtable (); // read a row of data from the file (Fileline = stockinput.readline ())! = null) {// Decompose each row of data into tokens. tokenize = new stringTokenizer (file {id = tokenize.nextToken (); // Create a buffer name to place stock information = new stringbuffer (); while (tokenize.hasmoreToKens ()) {name.Append (tokenize.nextToken ()); if (tokenize.hasmoreToKens ()) {name.append ("");}} // to HashTable Filling the record stockinfo.put (id, name.tostring ());} catch (nullpointerException eXCPT) {system.err.println ("Filling data when filled with data:" EXCPT);} catch (nosuchelementexception except) {system.err. Println ("Invalid Data Record" "IN File:" EXCPT);}},},}, (filenotfoundexception eXCPT) {system.err.println ("Cannot find files: EXCPT); Return False;} catch (ooException eXCPT) {system.err.println ("I / O Fault:" EXCP T); Return False;} Return True;

Protected String getquote (string stockid) {string info;

// Get data info = (String) stockinfo.get from HashTable; if (info! = Null) return; @ e!! "

Public void Paint (GRAPHICS G) {g.drawstring ("Stock Code" Quoteid ":", 10, 60); g.drawstring ("Stock Name" "Front" "Opening" "highest" "Minimum" "Closed" "Transaction Quantity" "Transaction Cost", 10, 90); G.drawString (Quotename, 10, 120);}

Public void actionPerformed (ActionEvent EV) {string label = ev.getActionCommand (); if (Label.Equals)) {quoteid = stockid.getText (); if (quoteid! = null) quotename = getQuote (quoteid) Else quotename = "Please enter the stock code!"; Repaint ();}}} ---- Due to the inherent security, security restrictions, if you do not use the securityPermission or digital signature, Java programs do not have read Privileges of local files, in order to save space, this article does not discuss this, put it to a .html file in a .html file, directly using the AppletViewer provided by JDK1.2, the method of use of its command line is as follows:

D: /jdk1.2/bin/appletviewer stockquote.html

转载请注明原文地址:https://www.9cbs.com/read-124947.html

New Post(0)