Differences between HashTable, HashSet, and Dictionary

Source: Internet
Author: User

I went to the interview again today, and the results were still miserable. I usually paid too much attention to the superficial things, but I was not very clear about them. I recorded HashTable and Dictionary-related knowledge, I hope it will be helpful to later users and can be used as a reference for future review.

  1. HashTable

A hash table (HashTable) indicates a set of key/value pairs. In. in the. NET Framework, Hashtable is System. A container provided by the Collections namespace is used to process and present key-value pairs similar to key-value. The key is usually used for quick search, and the key is case sensitive; value is used to store the value corresponding to the key. In Hashtable, key-value pairs are of the object type, so Hashtable can support any type of keyvalue pairs, and any non-null objects can be used as keys or values.

Add a key/key-value pair to the hash table: HashtableObject. Add (key ,);

Remove a key/key-value pair in the hash table: HashtableObject. Remove (key );

Remove all elements from the hash table: HashtableObject. Clear ();

Determine whether the hash table Contains the specified key: HashtableObject. Contains (key );

  2. HashSet

The HashSet <T> class is mainly designed for performing high-performance set operations, such as intersection, union, and difference sets for two sets. A set of elements that do not repeat and have no feature order. HashSet rejects repeated objects.

Some features of HashSet <T> are as follows:

Values in a. HashSet <T> cannot be repeated and there is no sequence.

The capacity of B. HashSet <T> is automatically added as needed.

  3. Dictionary

Dictionary indicates a set of keys and values.

Dictionary <string, string> is a generic

It has a set function. Sometimes it can be viewed as an array.

Its structure is as follows: Dictionary <[key], [value]>

It is characteristic that the stored object needs to be saved to the generic type one-to-one correspondence with the [key] value.

Find the corresponding value through a certain [key]

  4. Differences between HashTable and Dictionary:

(1). HashTable does not support generics, while Dictionary supports generics.

(2 ). hashtable elements belong to the Object type. Therefore, the binning and unboxing operations are often generated during storage or retrieval of value types. Therefore, you may need to perform some type conversion operations, and for int, float value types also require packing and other operations, which is time consuming.

(3). Dictionary is recommended in a single-threaded program. It has the advantage of generics, fast reading speed, and sufficient capacity utilization. Hashtable is recommended in multi-threaded programs. The default Hashtable allows single-threaded writing and multi-threaded reading. For Hashtable, the Synchronized () method can be further called to obtain the type of full thread security. dictionary is NOT thread-safe and must be manually protected using the lock statement, greatly reducing the efficiency.

(4) During code testing, it is found that the efficiency of keys being integer-type Dictionary is faster than that of Hashtable. If the key is string-type, the efficiency of Dictionary is not as fast as that of Hashtable.

  count = <, > dictionary =  Dictionary<, >=  ( i = ; i < count; i++= ( i = ; i < count; i++ value == ( i = ; i < count; i++ value =  count = <, > dictionary =  Dictionary<, >= ( i = ; i < count; i++= ( i = ; i < count; i++ value== ( i = ; i < count; i++ value =

  

The biggest achievement of today's interview is not to sum up the above, but to let me understand that many things should not be superficial, not to complete what functions, but to learn a certain technology, it is important to have an in-depth understanding of its nature. The three interviewers are very good. They talked about a lot of things that I usually seldom use. They can communicate with them and learn a lot. I really hope to enter a company like this in the future, let yourself grow up quickly.

The interview not only aims to find a satisfactory job, but also allows you to learn how to communicate and promote yourself. What's more, let yourself know about your shortcomings, I still need to improve my skills.

Know the market, know the problem, know yourself.

Continue to challenge tomorrow. hope everything goes well !!!

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.