In this section, we will learn the second set. Because of its characteristics, it can provide a very efficient search method, so it is very practical in actual projects, and it is a hash table. Hash inherits the idictionary interface,IdictionaryThe interface provides a design mode for the key (key)/value (value) set. Each object in this type set contains a key corresponding to it, you can use the specified key to find the corresponding object (value) in the set. The most important thing of this interface is to define the public attributes item, values, keys, here, item returns the value corresponding to the set based on the specified key, values is used to return all the object elements in the Set, and keys returns the key of the set, now you can think of the key as the "index value" of the stored value. The value can be found through this "index value. In addition to the Access Mechanism of key-value pairs in the hash table, we will also learn a sortlist class in the next section, which is the combination of key/value entries and an arraylist set, binary Search is implemented, and the performance is more powerful. Next, let's take a look at what is the access mechanism for key-value pairs, and use instances to understand the hash and hashtable classes.
HashA data structure similar to a dictionary is provided. The principle is that the hash function is an image, that is, the set of keywords is mapped to an address set, and its settings are flexible, as long as the size of this address set does not exceed the permitted range. Convert the key value of an element in a set to the index value of the corresponding table by using the hash function. The table of this type is called a hash table, the key value corresponds to a value called a hash code, and then the index address formed by the hash code is used to find the specified object element, that is, the value. Next, let's take a look at the knowledge points of hash:
A hash table is a set of key/value pairs.CodeOrganization, and corresponding values can be found based on the key.
I will not explain this point any more. What you should remember is that the internal mechanism of the hash operation is essentially a key operation.
The key cannot be empty or duplicate, and the value can be. Keys and values can be of any data type.
Because it is a key operation, the key value must be unique and any data type can be stored in the hash table, in addition, key-value pairs are operated strictly according to their data types. For example, if the key value is integer 1 and string type 1, the index address of the searched value is different, which must be strictly enforced.
Dynamically store key/value pairs. The capacity is automatically increased as needed.
The capacity of the Set object created by the hashtable class is automatically increased based on requirements. Its expansion is more scientific than that of arraylist and will not multiply, but determined by the loading factor, this factor determines how many elements the set fills in before continuing to expand its capacity.
The syntax for defining hash table objects isHashtable hstb = new hashtable ();
Common attributes
The number of elements in the key-value pair in the hstb. Count hash table is the same as that in arraylist. We mainly understand the attributes of the following two hashtable classes:
hstb. Keys: obtains the key value set in the set. hstb. Values returns the relative value set object. These two are obtained based on the elements of the object. They are read-only attributes and cannot be modified.
common method
Add element -there are two ways to add a hash table element, the first type is hstb. add (Key, value) must be added in the form of key-value pairs separated by "," ; the second method is to add a value in the form of a key. You can also modify the value in the specified key and store it in hstb, the format is hstb [Key] = value; . Pay attention to the key and value data types.
Delete element -two hstb types are used to delete an element. clear (); delete all elements. The second is to delete hstb. remove (key);
Search element - hstb. contains (key), hstb. containskey (key) to check whether the specified key is included.
hstb. whether containsvalue (value) contains the specified value, returns true or false, reflecting the query result.
Traversal Element-Traversal can be performed in two forms: the hash element is a key-value pair., Dictionaryentry typeIs a set of key-value pairs. This type of object is used to traverse hstb objects.
For example:Foreach (dictionaryentry jzd in hstb)The retrieved object is a hash key-value pair, and then the key is obtained using jzd. Key, and the value is obtained by jzd. value.
The second traversal is to traverse each key or value of the set of keys or values, suchForeach (Object K in hstb. Keys) orForeach (Object V in hstb. values)
Note: because the data type of the key or value can be any type, the Data Type of the variable that stores the key or value is the best after the collection is traversed.Object Type, Which contains any type.
In a hash class, the elements cannot be sorted according to the Data Type and key value arrangement principle, so we cannot obtain the specified elements in a specific order. The following example shows how to use the attributes and methods of a hash table.
1 Hashtable hstb = New Hashtable ();
2 Console. writeline ( " Adding element to the hstb set! Pay attention to the key and value data type! " );
3 Hstb. Add ( 1 , 11 ); // The value type is integer.
4 Hstb. Add ( " 1 " , " My key is string 1 " );
5 Hstb. Add ( 5 , " My key is an integer of 5, which is larger than yours. It should be displayed first. " );
6 Hstb [ 3 ] = " I assign values using the get method. " ;
7 Console. writeline ( " {0} elements added to the hstb set! " , Hstb. Count );
8
9 Console. writeline ( " The result is as follows: " );
10
11 Console. writeline ( " \ T key \ tvalue " );
12 Foreach (Dictionaryentry jzd In Hstb)
13 {
14Console. writeline ("\ T {0} \ t {1}", Jzd. Key, jzd. value );
15}
16
The running result is as follows:
Adding element to the hstb set! Pay attention to the key and value data type!
Four elements are added to the hstb set!
The result is as follows:
Key Value
5. My key is an integer of 5, which is bigger than yours. It should be displayed first.
3. I assign values using the get method.
1. My key is "1"
1 11
Press any key to continue...
Next we will add the following code to the above example to demonstrate how to modify the element value with the key value 3, and then traverse the key value using the key set.
Console. writeline ("modifying the key to 3 element value! ");
Hstb [3] = "you can modify your value in the retrieval method, haha! ";
Console. writeline ("Use a key set to traverse the output key and value ");
Console. writeline ("\ t key \ tvalue ");
Foreach (Object K in hstb. Keys)
{
Console. writeline ("\ t {0} \ t {1}", K, hstb [k]);
}
After K is traversed, K stores the key of the element and obtains the value through the key.
The running results are compared as follows:
The running results are compared as follows:
Adding element to the hstb set! Pay attention to the key and value data type!
Four elements are added to the hstb set!
The result is as follows:
Key Value
5. My key is an integer of 5, which is bigger than yours. It should be displayed first.
3. I assign values using the get method.
1. My key is "1"
1 11
Modifying the element value with the key 3!
Use a key set to traverse the output key and Value
Key Value
5. My key is an integer of 5, which is bigger than yours. It should be displayed first.
3. You can also modify your value for the retrieval method. Haha!
1. My key is "1"
1 11
Press any key to continue...
Let's take a look at the modified element value with the key 3. Next, we will demonstrate how to delete an element by the key, and then traverse the element value using the value set. Note: this traversal does not get the value of the element key because the hash code is organized by the key. Now we add the following code in the preceding example:
Code
Console. writeline ( " Deleting element whose key is integer 1 " );
Hstb. Remove ( 1 ); // Delete an element with key = 1
Console. writeline ( " Use a set of values to traverse the hash " );
Console. writeline ( " \ T key \ tvalue " );
Foreach ( Object V In Hstb. values)
{
Console. writeline ( " \ T {0} " , V );
} Only part of the code above will be intercepted for comparison.
the element value whose key is 3 is being modified!
use a key set to traverse the output key and value
5. My key is an integer of 5, which is larger than yours,
3 is displayed first. You can modify the value of this method. Haha!
1 My key is "1"
1 11
deleting an element whose key is integer 1
traversing the hash using a set of values
value
my key is an integer of 5, if it is larger than yours, you should first display
the acquisition method can also modify your value, haha!
my key is "1"
press any key to continue...
Through the example below, I believe you are already familiar with its use. I will leave a running result for the assignment question below.
Create a student management system using a hash table
Welcome to the 0122 course student registration system
==========================================================
Select the following actions
A: Add B: Query C: delete D: Modify E: exit the system
A
Enter the student ID: 1
Enter the Student name: Tang laoduck
*******************************
Select the following actions
A: Add B: Query C: delete D: Modify E: exit the system
A
Enter the student ID: 2
Enter the Student name: crayon
*******************************
Select the following actions
A: Add B: Query C: delete D: Modify E: exit the system
B
Select a for student ID Search and B for name search
A
Enter the student ID to query:
1
Student name: Donald Duck
A: Continue query B: Exit Query
B
==========================================================
Select the following actions
A: Add B: Query C: delete D: Modify E: exit the system
D
This job is relatively simple. The student ID is stored with a key. The value stores a variable name. Normally, the value stores more information in the form of an array or a dynamic array, for example, age, contact information, and so on. Then, you can use the index of the array in the value to find a single information and modify the information. This method is also a common practice in actual projects, such as online mall, A new item information is added. Hash Tables are usually used to implement functions. In the next section, we will learn binary search and sortlist classes.