This article is a mirror article of machine translation, please click here to jump to the original article.

View: 24620|Reply: 2

[Source] .net/c# HashSet to add unique objects

[Copy link]
Posted on 9/7/2017 11:12:10 AM | | | |
.NET 3.5 includes a new collection class in the System.Collections.Generic namespace: HashSet<T>. This collection class contains an unordered list of unique items. This set is called a "set". Set is a reserved word, so the class has another name, HashSet<T>. The name is easy to understand because this collection is based on hash values, and inserting elements is very fast and does not require <T>rearrangement of the collection like the List class.
We prepare 3 collections, and the objects in list1 are not processed. The objects in list2 override some methods, and list3 contains string types



The code is as follows:

1. If the hash code value is not the same, it means that it is a new element, store;

2,If the hash code value is the same and the equles are judged to be equal, it means that the element already exists and does not exist

3. If the hash code value is the same and the equles judgment is not equal, it means that the element does not exist and exists;



We use the Test2 object, which overrides the object's equals and hashCode methods. Here, let the Test2 object be considered the same instance as long as the id and a are the same, and of course it can be something else, it depends on the specific requirements




Previous:Google Adsense modifies the PIN code to receive the address
Next:What is the reason for your registration bar?
Posted on 9/8/2017 8:53:26 AM |
The interval is like a mountain, and those who selflessly dedicate themselves will live forever
Posted on 9/8/2017 3:01:50 PM |
Landlord, you haven't finished writing, have you?
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com