Set
A collection of non-repeating elements is called a set. . The NET4 contains two sets (hashset<t> and sortedset<t>) and They all implement iset<t > interface . Hashset<t> is an unordered list that contains non-repeating elements ,sortedset<t> The set contains an ordered list of non-repeating elements .
the Iset<t> interface provides methods that can create collections, intersections , or information that gives a superset or subset of another set when one is set .
Case :
use HashSet: Duplicate elements are automatically removed , but not sorted
var set = new Hashset<int> () {5, 9, 2, 1, 2, 2, 3, 7, 4, 9, 9};
foreach (var item in set)
{
Console.WriteLine (item);
}
Console.readkey ();
The same Code , replace the HashSet with SortedSet:
use SortedSet: Duplicate elements are automatically removed and sorted
var set = new Sortedset<int> () {5, 9, 2, 1, 2, 2, 3, 7, 4, 9, 9};
foreach (var item in set)
{
Console.WriteLine (item);
}
Console.readkey ();
Summary :
The main function of 1.HashSet and SortedSet is to make two sets of intersection , set , difference set and other operations . The collection contains a set of elements that do not recur and have no attribute order . The former does not automatically sort , The latter is added after the element , Automatic sorting
2. Neither of them can access one of the elements from a specific location .
3. You can use its find function :
Set.contains ("value")// returns true or false
4. Operations on a collection :
A. Symmetricexceptwith: contains only the elements that exist in the object or the specified collection ( but not the elements in both ). Remove Intersection , two remaining set elements .
B. Unionwith: contains the object itself and formulates all the elements that exist in the collection . and set
C. Exceptwith removes all elements from the specified collection from the current hashset<t> object . Difference Set
D. Intersectwith: contains only the object and the elements that exist in the specified collection . intersection
5.SortedSet Object , You can call the getviewbetween,max,min method
6. In addition to SortedSet , the System.Collections.Generic namespace provides SortedDictionary and SortedList two classes .
some of the methods built into the test HashSet :
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Namespace Set
{
Class Program
{
static void Main (string[] args)
{
hashset<char> SetA = new hashset<char> ();
hashset<char> Setb = new hashset<char> ();
Seta.add (' A ');
Seta.add (' B ');
Seta.add (' C ');
Setb.add (' C ');
Setb.add (' D ');
Setb.add (' E ');
Show ("Initial content of SetA:", SetA);
Show ("Initial content of Setb:", SETB);
Seta.symmetricexceptwith (SETB); list setA,setb , and other elements that are not unique to each other.
Show ("SetA after symmetric difference with Setb:", SetA);
Seta.unionwith (SETB); List all the elements of SetAandSETB (Union set )
Show ("SetA after union with SETB:", SetA);
Seta.exceptwith (SETB); Remove the setb elements that are in the SetA
Show ("SetA after subtracting Setb:", SetA);
Console.WriteLine ();
Console.read ();
}
static void Show (String msg, hashset<char> set)
{
Console.Write (msg);
foreach (char ch in set)
Console.Write (ch + "");
Console.WriteLine ();
}
}
}
methods for testing SortedSet :
Using System;
Using System.Collections.Generic;
Using system.linq;// This is the necessary call for the Max (), Min () method
Using System.Text;
Using System.Threading.Tasks;
Namespace Set
{
Class Program
{
static void Main (string[] args)
{
var set = new Sortedset<int> () {5, 9, 2, 1, 2, 2, 3, 7, 4, 9, 9};
foreach (int element in set)
Console.WriteLine (String. Format ("{0}", Element));
Console.WriteLine ("Max:" + set.) Max ());
Console.WriteLine ("Min:" + set.) Min ());
Console.Write ("<br> take 2 ~ 5 value :");
only elements with values between 2 and 5 are taken
var subset = set. Getviewbetween (2, 5);
foreach (int i in subset)
{
Console.Write (i + "");
}
Console.WriteLine ();
Console.read ();
}
static void Show (String msg, hashset<char> set)
{
Console.Write (msg);
foreach (char ch in set)
Console.Write (ch + "");
Console.WriteLine ();
}
}
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C # Advanced Programming 55 days----hashset and SortedSet