Java uses the hash table to realize the stock market

zhaozj2021-02-08  406

---- 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 relationship H (called a hash 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 private Hashtable stockInfo;; TextField stockID; Button button1; private String quoteid, quotename; public void init () {add (new Label ( "ticker")); stockID = new TextField (= new ( "hqsj.txt") File 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 an access data files streamBufferedReader stockInput = new BufferedReader (new FileReader (INFO_FILE)); // create a Hashtable object stockInfo = new Hashtable ( ); // read a row of data from the file (FileLine = stockinput.readline ())! = Null) {// Decompose each line of data to tokens.tokenize = new StringTokenizer (fileline); try {id = Tokenize.nextToken (); // Create a buffername = new stringbuffer () (tokenize.hasmoreToKens ()) {n Ame.Append (tokenize.nextToken ()); if (tokenize.hasmoretoKens ()) {name.append ("");}} // Fills the record in HashTable. } catch (nullpointerException eXCPT) {system.err.println ("Filled data when filled:" EXCPT);} catch (nosuchelementexception except) {system.err.println ("invalid data record" "in file:" EXCPT);}} stockinput.close ();} catch (filenotfoundexception eXCPT) {system.err.println ("Cannot find files: EXCPT); Return False;} catch (ooException except) {system.err.println "I / O Fault:" EXCPT); Return False;} Return True;} Protected String getQuote (String stockid) {string info; // Get data info from HashTable

(STRING); if (info! = Null) return; elsereturn "stock code error!";} Public void Paint (graphics g) {g.drawstring ("stock code" quoteid ":" 10, 60); g.drawstring ("Stock Name" "Front Receive" "Opening" "Maximum" "Minimum" "Closed" "Transaction Volume" "Transaction Amount", 10, 90 ); g.drawstring (quotename, 10, 120);} public void actionperformed (ActionEvent EV) {string label = ev.getActionCommand (); if (label.equals ("query)) {quoteid = stockid.getText () ; if (quoteid! = null) quotename = getquote (quote quotename = "Please enter the stock code!"; repaint ();}}} ---- Due to the inherent Java, security restrictions, if not used SECURITYPERMISSION or digital signature, Java programs do not have permission to read local files, in order to save space, this article does not discuss this, put the compiled stock Quit.class in one .html file, use JDK1 directly .2 supplied AppletViewer, the method of use of its command line is as follows:

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

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

New Post(0)