C # Introduction to HashSet Collection types

Source: Internet
Author: User

1. HashSet set

HashSet is based on a mathematical Set. Using HashSet can improve the operation of a Set. The HashSet set does not contain the sorting method. If you want to sort the data, see use List The collection works with the Sort method.

The advantages of HashSet are fast operation. As a type of data stored in the memory, you can quickly set and set values. You cannot add duplicate data to a HashSet to avoid adding a HashSet. The data is duplicated. We use HashSet to subtract the operations between these sets and the set in the Set addition set.

When HashSet is used as a fast database for memory storage, the data in the new HashSet must be followed at any time because the system automatically recycles a data that has not been accessed for a long time in the HashSet, so it will lead to failure. How can we ensure that the values in the HashSet are long and continuously update the values?

First, the program comes to access our HashSet and there is no required data. If there is any data we need, it will be directly returned to the user without calling the database query operation. If there is no data in the HashSet, the program will Query whether the database has the Query data again. If any data is returned to the user, the Query results will be added to the HashSet, this can reduce the inconvenience caused by database query to a certain extent, but it cannot be eliminated. To further improve the performance, you can view the previous Cache Policy and use memcached to improve website query and access.

1. We need the union of the two Sets

HashSet
  
   
Numbers1; HashSet
   
    
Numbers2; // initialize the values of numbers1 and numbers2, respectively, or assign the values numbers1.UnionWith (numbers2); // calculate the union of the two sets.
   
  

. Calculate the intersection of two sets

HashSet
  
   
Numbers1; HashSet
   
    
Numbers2; // initialize the values of numbers1 and numbers2, respectively, or assign the value numbers1.IntersectWith (numbers2); // calculates the intersection of the two sets.
   
  

. Calculate the difference set of the Two Sets

HashSet
  
   
Numbers1; HashSet
   
    
Numbers2; // initialize the values of numbers1 and numbers2 respectively or assign the value numbers1.20.twith (numbers2); // calculate the difference set of the two sets.
   
  

. Calculate the symmetric difference set of the Two Sets

HashSet
  
   
Numbers1; HashSet
   
    
Numbers2; // initialize the values of numbers1 and numbers2, respectively, or assign the value numbers1.w.ricw.twith (numbers2); // obtain the symmetric difference set of the two sets.
   
  

2. Common HashSet extension methods

2.1.Add: add the project to the HashSet.

2.2.Clear: clears the values in the HashSet.

2.3.Remove: remove the value from the HashSet.

2.4 Contains: determines whether the HashSet Contains the specified project.

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

3. HashSet and Linq operations

HashSet Method Equivalent to LINQ

UnionWith ==> Union

IntersectWith ==> Interset

ExceptWith ==> cipher t

Sorting is not provided ==> Distinct

So we already have a lot of set types, such as List , Dictionary Or Hashtable and so on, how should we select which collection type I should use?

"If you must create new settings, or if the application only needs to access the provided tasks, use any IEnumerable The set is enough. However, if the application needs to access other values, or it is not recommended to create a new set, you can use HashSet . "-- From MSDN

Http://msdn.microsoft.com/zh-tw/library/bb397728 (v = vs.110). aspx



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.