C # HashSet Collection type usage Introduction

Source: Internet
Author: User

1.HashSet Collection

Using HashSet can improve the operation of the collection. Using the HashSet collection does not have a sorting method, if you need to sort the requirements you can refer to using the List<t> collection with the Sort method.

The advantage of HashSet is that it can be set up and evaluated quickly, as a kind of data stored in memory. HashSet cannot add duplicate data to the inside, avoiding the duplication of data in the hashset<t>. We use HashSet to subtract these collections from the collection in addition to the collection.

Using HashSet as a fast database for memory storage, this needs to keep up with the data in the new HashSet, because the data that has not been accessed in HashSet will be automatically reclaimed by the system, resulting in a failure. So how can we ensure that the value inside the hashset is long and that it reaches the constant update value?

First of all, the program came to visit us hashset there is no need for data, if we have the data we need to return directly to the user, do not call query database operation. If hashset there is no data we need, the program to query the database if there is the query data, if there is a return to the user at the same time to add the results of the query hashset inside, so that can be a certain extent to reduce the inconvenience of querying the database, but can not be eradicated, To further improve performance, you can view the previous cache policy using memcached to improve site query and access.

1.1. We require a combination of two sets

Hashset<int> numbers1; Hashset<int> numbers2;//numbers1 and numbers2 values are initialized or assigned numbers1 respectively. Unionwith (NUMBERS2);//ask for a set of two sets.

  

1.2. Find the intersection of two sets

Hashset<int> numbers1; Hashset<int> numbers2;//numbers1 and numbers2 values are initialized or assigned numbers1 respectively. Intersectwith (NUMBERS2);//Find the intersection of two sets.

  

1.3. Ask for a difference set of two sets

Hashset<int> numbers1; Hashset<int> numbers2;//numbers1 and numbers2 values are initialized or assigned numbers1 respectively. Exceptwith (NUMBERS2);//ask for a difference set of two sets.

  

1.4. Ask for a symmetric difference set of two sets

Hashset<int> numbers1; Hashset<int> numbers2;//numbers1 and numbers2 values are initialized or assigned numbers1 respectively. Symmetricexceptwith (NUMBERS2);//ask for a symmetric difference set of two sets.

  

2.HashSet Common extension methods

2.1.ADD, add the project to HashSet.

2.2.Clear, empty the value inside the hashset.

2.3.Remove, remove the value from the HashSet.

2.4Contains to determine whether HashSet contains the specified item.

2.4Equals (Object) to determine if it is equal.

3.HashSet and LINQ Operations

Hashset<t> method of equivalence with LINQ

Unionwith ==> Union

Intersectwith ==> Interset

Exceptwith ==> Except

Do not provide sort ==> Distinct

So we already have a lot of collection types such as List<t>,dictionary<tkey,tvalue> or Hashtable, and how do we choose which collection type I should take?

"If you have to set up a new setting, or if the application only needs to access the provided tasks, then using any of the ienumerable<t> collections is sufficient." However, you can use HASHSET<T> if your application needs to access other values, or if you do not recommend that you need to create a new collection;. " --Excerpt from MSDN

C # HashSet Collection type usage Introduction

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.