A detailed explanation of the new features of the SortedSet class. NET 4.0

Source: Internet
Author: User
Tags foreach data structures hash sort

Microsoft has added a HashSet class to. NET 3.5, adding a SortedSet class to. NET 4, which describes their features and compares their similarities and differences.

The HashSet and SortedSet classes of the. NET Collection function Library implement the System.Collections.Generic.ISet interface, but Java versions earlier than 1.2 (or earlier) have provided implementations that Two kinds of data structures have the same name class, and there are more rigorous TreeSet (the items stored inside, even the type must be consistent.) Did not have a generic type).

Set is the meaning of "set", which is mathematically defined as having no specific order in which elements are stored, and which does not allow duplication. Let's look at the following hashset, SortedSet examples:

ISet set = new HashSet() { 5, 9, 2, 1, 2, 2, 3, 7, 4, 9, 9 };
foreach (int element in set)
Response.Write(string.Format(" {0}", element));

Execution results:

Figure 1 The duplicated elements are automatically removed

The same code, change the HashSet to SortedSet, as follows:

ISet set = new SortedSet() { 5, 9, 2, 1, 2, 2, 3, 7, 4, 9, 9 };
foreach (int element in set)
Response.Write(string.Format(" {0}", element));

Execution results:

Figure 2 The duplicated elements are automatically removed and the interior is automatically sorted

We see that the two classes of hashset and SortedSet do not allow duplicates, but the former are stored in the order in which they are added, which then sorts the added elements and maintains the order of the data when inserting, deleting, and searching for elements. So if you have the patience to read here, you'll learn a trick: If you want to filter repetitive items in a normal programming way, you can use these two set classes because the collection is not allowed to repeat elements. In the. NET 3.5 era, where SortedSet has not yet appeared, we must remove duplicate items and then sort them by HashSet, and now. NET 4, we can use the SortedSet one-step implementation to remove duplicates and sort.

Of course, if your requirements are different (regardless of performance differences), you do not want to automatically sort by default, or you do not need to remove duplicate elements, you can use the sort method of the List class instead. In addition, you can also use HashSet as a key/value pairing of the Dictionary class, only the key does not have value, because Dictionary (generic HashTable) its characteristics, and hastset similar, element is not special Ordered, and no duplicates are allowed (key must be unique).

The following features are listed on the. NET Platform for each of the two classes of HashSet, SortedSet:

Characteristics of Hastset:

Its Contains method, which determines whether the HashSet object contains the specified element, is executed quickly because it is based on a hash lookup (hash-based lookup).

(Another Dictionary class retrieval speed is also very fast, its "algorithm" time complexity close to O (1), because the Dictionary class is implemented as a hash table)

Its Add method (adds the specified element to the HashSet object), and if Count is less than the capacity of the inner array, this method has an O (1) Operation complexity. If the size of the HashSet object must be resized, the method's operational complexity will be O (n).

It cannot store duplicate elements, and is automatically ignored when the inserted elements have duplicates.

You cannot access one of these elements from a specific location.

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.