Introduction to Collection Sets
Introduction to Collection Sets
Fundamentals of a Set
Introduction
A set is a collection of values or objects so the list must respond to some Boolean conditions compared to another set. One operation would consist of finding whether two collections are the same because they respond to the same condition. Another operation would consist of finding out whether one list contains the same items as another but contains fewer items than the other collection, and so on. There are rules and conditions a set must follow:
Starting a Set
Consider the following list and notice that it contains an item twice:
<!DOCTYPE html> <html> <head> <title>Names</title> </head> <body> <h2>Names</h2> @{ System.Collections.Generic.List<string> names = new System.Collections.Generic.List<string>(); names.Add("Kevin"); names.Add("Jane"); names.Add("Daniel"); names.Add("Jane"); names.Add("Ruth"); names.Add("Paul"); } <ul> @foreach(string name in names) { <li>@name</li> } </ul> </body> </html>
This would produce:
On of the general rules of a set is that its list should not allow duplicate items (this rule is not strictly followed in algebra but must be followed here).
To support sets, the .NET Framework provides a class named HashSet. This class is defined in the System.Collections.Generic namespace. The HashSet class starts as follows:
public class HashSet<T> : ICollection<T>, IEnumerable<T>, IEnumerable, ISerializable, IDeserializationCallback
Notice that the HashSet class implements:
Most of the time, in algebra, to study sets, we use numbers. In all of the examples in this lesson, we will use strings to make them more interesting. Obviously, integers would be easier. If you want to use numbers, all you have to do is to replace all strings in our examples with numbers. |
Creating an Empty Set
In algebra, a set is empty if it contains no element. This is the case for a brand new set. It can also happen if the elements in the set have been deleted. In algebra, to create an empty set, use an uppercase letter and assign the phi character to it. Here is an example:
A = ∅
(In algebra, an empty set is also called a null set.) In algebra, to visually represent a set, we draw a circle. If the set is empty, the circle is blank:
To programmatically create an empty set, declare a variable of type HashSet and use the new operator to initialize it.
Depending on the type of set, one set can be made of natural numbers, another made of floating-point numbers, yet another one made of strings or other types of values (dates, times, etc). When programmatically creating a set, pass the type of items in the angle brackets. Here is an example:
@{
System.Collections.Generic.HashSet<string> names = new System.Collections.Generic.HashSet<string>();
}
Fundamental Operations on a Set
Populating a Set
Unless a set is empty, it must have or contain something. An object included in a set is called an element or a member of the set. In algebra, to create a set, we specify an uppercase letter, followed by = { }. In the curly brackets, we list the elements separated by commas. Here is an example:
A = { Kevin, Jane, Daniel, Ruth, Paul }
To visually illustrate, you can represent each element in the circle. Here is an example:
To let you add an item to a set, the HashSet class is equipped with a method named Add. Its syntax is:
public bool Add(T item);
Pass the desired value as argument to this method. Remember that the class implements the IEnumerable<> interface and therefore the foreachloop. Here are examples of calling the Add() method to populate a set:
<!DOCTYPE html>
<html>
<head>
<title>Names</title>
</head>
<body>
<h2>Names</h2>
@{
System.Collections.Generic.HashSet<string> names = new System.Collections.Generic.HashSet<string>();
names.Add("Kevin");
names.Add("Jane");
names.Add("Daniel");
names.Add("Jane");
names.Add("Ruth");
names.Add("Paul");
}
</body>
</html>
Notice that each element in this case is a constant, or has a constant value.
Remember that one of the rules of a set is that it cannot have duplicate items. If you add an item that exists in the list already, that item would not be added. In fact, to let you know whether an item was added, the HashSet<>.Add() method returns a Boolean value:
Accessing Each Element of a Set
The HashSet class implements the IEnumerable<T> and the IEnumerable interfaces. This makes it possible to use the foreach loop to access each item of a set. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Playground Accessories</title>
</head>
<body>
<h2>Playground Accessories</h2>
@{
System.Collections.Generic.HashSet<string> types = new System.Collections.Generic.HashSet<string>();
types.Add("Swing");
types.Add("Seesaw");
types.Add("Jungle Gym");
types.Add("Spring Rider");
types.Add("Chin-Up Bars");
}
<ul>
@foreach (string equipment in types)
{
<li>@equipment</li>
}
</ul>
</body>
</html>
This would produce:
The Number of Items in a Set
In algebra, when it comes to the number of elements in a set, there are two categories. A set is referred to as finite if it contains a constant number of items. An example is the number of students in a classroom. A set is infinite if its number of items cannot be determined (or the items cannot be counted). An example is the number of stars in the sky.
The HashSet class implements the ICollection<> generic interface, which gives it a property named Count. This property allows you to know the number of elements in a set. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Names</title>
</head>
<body>
@{
System.Collections.Generic.HashSet<string> names = new System.Collections.Generic.HashSet<string>();
names.Add("Kevin");
names.Add("Jane");
names.Add("Daniel");
names.Add("Jane");
names.Add("Ruth");
names.Add("Paul");
names.Add("Jane");
}
<p>This set contains @names.Count elemnts.</p>
</body>
</html>
This would produce:
Notice that the property produces the actual number of items in the collection, excluding those that were not added (because of duplication issues).
Checking Whether a Set Contains a Certain Element
In algebra, to represent that a certain value "a" exists in a set A, we would write:
a ∈ A
This is read as "a is an element of set A" or "a is included in set A" or "Set A contains a". To support this operation, the HashSet class inherits the Contains() method from the ICollection<> interface. The method allows you to check whether the list already contains a certain item. Here are examples of calling this method:
<!DOCTYPE html> <html> <head> <title>Names</title> </head> <body> @{ System.Collections.Generic.HashSet<string> names = new System.Collections.Generic.HashSet<string>(); names.Add("Kevin"); names.Add("Jane"); names.Add("Daniel"); names.Add("Jennifer"); names.Add("Ruth"); names.Add("Paul"); } <p>This set contains Jane: @names.Contains("Jane").</p> <p>This set contains Frank: @names.Contains("Frank").</p> </body> </html>
This would produce:
In algebra, to state that an element a is not in the set A, we write:
a ∉ A
This reads "a is not an element of set A" or "a is not included in set A" or "Set A does not contain a". To perform this checking in programming, you can use the NOT operator "!".
Creating a Duplicate Set
In algebra, after creating a set A, you may want to create another set B that contains the same elements as A. To do this, you can simply assign the elements of A to B. This would be done as follows:
A = { Kevin Walchild, Jane Overton, Daniel Albertson,
Ruth Oyawale, Jane Ouelette, Paul Sullivan }
B = A
Now, sets A and B have the same elements. This can be illustrated as follows:
To let you assign the values of one HashSet collection to another, the class provides another constructor. Its syntax is:
public HashSet(IEnumerable<T> collection);
This constructor allows you to create a new set that is made of values from an existing set. You create the new list by passing the name of the existing list to this constructor. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Playground Accessories</title>
</head>
<body>
<h2>Playground Accessories</h2>
@{
System.Collections.Generic.HashSet<string> types = new System.Collections.Generic.HashSet<string>();
types.Add("Swing");
types.Add("Seesaw");
types.Add("Jungle Gym");
types.Add("Spring Rider");
types.Add("Chin-Up Bars");
System.Collections.Generic.HashSet<string> recreation = new System.Collections.Generic.HashSet<string>(types);
}
<ul>
@foreach (string obj in recreation)
{
<li>@obj</li>
}
</ul>
</body>
</html>
Once you have created the list, you can manipulate it as you see fit. For example, you can add new items to it. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Playground Accessories</title>
</head>
<body>
<h2>Playground Accessories</h2>
@{
System.Collections.Generic.HashSet<string> types = new System.Collections.Generic.HashSet<string>();
types.Add("Swing");
types.Add("Seesaw");
types.Add("Jungle Gym");
types.Add("Spring Rider");
types.Add("Chin-Up Bars");
System.Collections.Generic.HashSet<string> recreation = new System.Collections.Generic.HashSet<string>(types);
recreation.Add("Playground Slide");
recreation.Add("Merry-Go-Round");
}
<ul>
@foreach (string type in recreation)
{
<li>@type</li>
}
</ul>
</body>
</html>
This would produce:
Operations on Sets
Comparing Sets For Equality
Imagine you have two sets A and B defined as follows:
A = { Connecticut, Maine, Massachusetts, New Hampshire, Rhode Island, Vermont } B = { Massachusetts, Vermont, Connecticut, New Hampshire, Rhode Island, Maine }
Notice that both sets have the same elements. When two sets have exact same members, we say that those sets are equal. This can be expressed as:
A = B
This operation is commutative, which means the above operation can also be written as:
B = A
We already know that all classes used in a C# application (or a .NET project) derive from the Object class and they inherit a method named Equals. This makes it possible to compare two values or two objects for equality. The HashSet class also naturally inherits this method. Although you can use that method, the HashSet class provides its own custom method named SetEquals. Its syntax is:
public bool SetEquals(IEnumerable<T> other);
This method takes as argument another HashSet list and compares both sets. If the sets are the same, the method returns true. Otherwise it produces a false value. Here are examples of calling this method:
<!DOCTYPE html> <html> <head> <title>Country's Regions</title> </head> <body> <h2>Country's Regions</h2> @{ System.Collections.Generic.HashSet<string> census = new System.Collections.Generic.HashSet<string>(); System.Collections.Generic.HashSet<string> omb = new System.Collections.Generic.HashSet<string>(); System.Collections.Generic.HashSet<string> eastCoast = new System.Collections.Generic.HashSet<string>(); census.Add("Connecticut"); census.Add("Maine"); census.Add("Massachusetts"); census.Add("New Hampshire"); census.Add("Rhode Island"); census.Add("Vermont"); omb.Add("Massachusetts"); omb.Add("Vermont"); omb.Add("Connecticut"); omb.Add("New Hampshire"); omb.Add("Rhode Island"); omb.Add("Maine"); eastCoast.Add("New Hampshire"); eastCoast.Add("Vermont"); eastCoast.Add("Massachusetts"); eastCoast.Add("Rhode Island"); eastCoast.Add("Connecticut"); eastCoast.Add("New Jersey"); eastCoast.Add("Delaware"); eastCoast.Add("Maryland"); eastCoast.Add("Virginia"); eastCoast.Add("North Carolina"); eastCoast.Add("South Carolina"); eastCoast.Add("Georgia"); eastCoast.Add("Florida"); } @if (census.SetEquals(omb) == true) { <p>The Census Bureau and the Office of Management and Budget use the same north-east states for their first statistical region.</p> } else { <p>The Census Bureau and the Office of Management and Budget use different states for their statistical analyses.</p> } @if (census.SetEquals(eastCoast) == true) { <p>The Census Bureau uses all of the states of the east coast for its projections.</p> } else { <p>The Census Bureau uses only some of the coastal states for statistical purposes.</p> } </p> </body> </html>
This would produce:
A Subset of Another Set
Imagine you have two sets A and B defined as follows:
A = { New York, New Jersey, Pennsylvania } B = { Delaware, Pennsylvania, District of Columbia, New York, Maryland, New Jersey }
Notice that all members of A are also members of B. In algebra, to express this, we write:
A ⊂ B
This is the same as:
{ New York, New Jersey, Pennsylvania } ⊂ { Delaware, Pennsylvania, District of Columbia, New York, Maryland, New Jersey }
We say that A is a subset of B. This can be visually illustrated as follows:
In algebra, any set is a subset of itself. Also, since the empty set doesn't have any element, the empty set is a subset of any set. We write it as:
∅ ⊂ A
and
∅ ⊂ B
An empty set is also a subset of itself.
To let you find out you whether one set is a subset of another, the HashSet class is equipped with a method named IsSubsetOf. Its syntax is:
public bool IsSubsetOf(IEnumerable<T> other);
If the set passed as argument is a subset of the variable that is calling the method, it returns true. Otherwise, it returns false. Here is an example of calling this method:
<!DOCTYPE html>
<html>
<head>
<title>Country's Regions</title>
</head>
<body>
<h2>Country's Regions</h2>
@{
System.Collections.Generic.HashSet<string> censusMidAtlantic = new System.Collections.Generic.HashSet<string>();
System.Collections.Generic.HashSet<string> beaMideast = new System.Collections.Generic.HashSet<string>();
censusMidAtlantic.Add("New York");
censusMidAtlantic.Add("New Jersey");
censusMidAtlantic.Add("Pennsylvania");
beaMideast.Add("Delaware");
beaMideast.Add("Pennsylvania");
beaMideast.Add("District of Columbia");
beaMideast.Add("New York");
beaMideast.Add("Maryland");
beaMideast.Add("New Jersey");
}
@if(censusMidAtlantic.IsSubsetOf(beaMideast) == true)
{
<p>The Mid-Atlantic region of the Census Bureau is a sub-set of the Mideast region of the Bureau of Economic Analysis.</p>
}
else
{
<p>The Mideast region of the Bureau of Economic Analysis has more states than the Mid-Atlantic region of the Census Bureau.</p>
}
</p>
</body>
</html>
It is important to know that the subset relationship is one way; in other words, the comparison is not commutative: the fact that a set A is a subset of a set B is not vice-versa.
A Proper Subset of Another Set
A set is a subset of itself. Also, when two sets have the exact same members, each set is a subset of the other. Consider the following illustration of sets:
If you have a set A that is strictly a subset of another set B, this means there is at least one element in set B that is not a member of set A. In this case, we say that set A is a proper subset of set B. This can be illustrated as follows:
To let you find out if a set is a proper subset of another set, the HashSet class is equipped with a method named IsProperSubsetOf . Its syntax is:
public bool IsProperSubsetOf(IEnumerable<T> other);
This method takes a HashSet list as argument and performs a comparison on their elements:
Here are two examples of calling this method:
<!DOCTYPE html> <html> <head> <title>Country's Regions</title> </head> <body> <h2>Country's Regions</h2> @{ System.Collections.Generic.HashSet<string> censusMidAtlantic = new System.Collections.Generic.HashSet<string>(); System.Collections.Generic.HashSet<string> beaMideast = new System.Collections.Generic.HashSet<string>(); censusMidAtlantic.Add("New York"); censusMidAtlantic.Add("New Jersey"); censusMidAtlantic.Add("Pennsylvania"); beaMideast.Add("Pennsylvania"); beaMideast.Add("New York"); beaMideast.Add("New Jersey"); } @if(censusMidAtlantic.IsProperSubsetOf(beaMideast) == true) { <p>The Mid-Atlantic region of the Census Bureau is a proper sub-set of the Mideast region of the Bureau of Economic Analysis.</p> } else { <p>The Mid-Atlantic region of the Census Bureau is NOT a proper sub-set of the Mideast region of the Bureau of Economic Analysis.</p> } @{ beaMideast.Add("Delaware"); beaMideast.Add("District of Columbia"); beaMideast.Add("Maryland"); } @if (censusMidAtlantic.IsProperSubsetOf(beaMideast) == true) { <p>The Mid-Atlantic region of the Census Bureau is a proper sub-set of the Mideast region of the Bureau of Economic Analysis.</p> } else { <p>The Mid-Atlantic region of the Census Bureau is NOT a proper sub-set of the Mideast region of the Bureau of Economic Analysis.</p> } </body> </html> @if(censusMidAtlantic.IsProperSubsetOf(beaMideast) == true) { <p>The Mid-Atlantic region of the Census Bureau is a proper sub-set of the Mideast region of the Bureau of Economic Analysis.</p> } else { <p>The Mideast region of the Bureau of Economic Analysis has more states than the Mid-Atlantic region of the Census Bureau.</p> } </p> </body> </html>
This would produce:
A Super-Set of an Existing Set
Remember that a sub-set is a set whose all elements are also found in another set. A super-set is the reverse of a sub-set. That is, in a superset, all the elements of a set B are found in a set A but set A may have elements that are not found in B. In algebra, this can be written as follows:
B ⊃ A
To help you make this comparison, the HashSet class is equipped with a method named IsSupersetOf. Its syntax is:
public bool IsSupersetOf(IEnumerable<T> other);
This method takes a HashSet object as argument and compares its elements to those of the variable that called it. If all the elements of the argument are found in the variable that called it, the method returns true. Here are examples of calling this method:
<!DOCTYPE html> <html> <head> <title>Country's Regions</title> </head> <body> <h2>Country's Regions</h2> @{ System.Collections.Generic.HashSet<string> beaMideast = new System.Collections.Generic.HashSet<string>(); System.Collections.Generic.HashSet<string> standardRegion2 = new System.Collections.Generic.HashSet<string>(); System.Collections.Generic.HashSet<string> censusMidAtlantic = new System.Collections.Generic.HashSet<string>(); censusMidAtlantic.Add("New York"); censusMidAtlantic.Add("New Jersey"); censusMidAtlantic.Add("Pennsylvania"); beaMideast.Add("Pennsylvania"); beaMideast.Add("New York"); beaMideast.Add("New Jersey"); beaMideast.Add("Delaware"); beaMideast.Add("District of Columbia"); beaMideast.Add("Maryland"); standardRegion2.Add("New York"); standardRegion2.Add("New Jersey"); standardRegion2.Add("Puerto Rico"); standardRegion2.Add("US Virgin Islands"); } @if (beaMideast.IsSupersetOf(censusMidAtlantic) == true) { <p>The Mid-Atlantic region of the Bureau of Economic Analysis is a super-set of the Mideast region of the Census Bureau.</p> } else { <p>The Mid-Atlantic region of the Bureau of Economic Analysis is NOT a super-set of the Mideast region of the Census Bureau.</p> } @{ } @if (standardRegion2.IsSupersetOf(censusMidAtlantic) == true) { <p>Region II of the Office of Management and Budget (OMB) is a super-set of the Mideast region of the Census Bureau.</p> } else { <p>Region II of the Office of Management and Budget (OMB) is NOT a super-set of the Mideast region of the Census Bureau.</p> } </body> </html>
This would produce
A Proper Super-Set of an Existing Set
When it comes to a super-set, if both sets are the same, each is considered a super-set of the other and the IsSupersetOf() method returns true. By contrast, if a set B is a super-set of A but both sets are not the same, that is, set B has more elements than set A, set B is said to be a proper super-set of A. To let you make the comparison to determine this, the HashSet class provides a method named IsProperSupersetOf. Its syntax is:
public bool IsProperSupersetOf(IEnumerable<T> other);
Here is an example:
<!DOCTYPE html> <html> <head> <title>Country's Regions</title> </head> <body> <h2>Country's Regions</h2> @{ System.Collections.Generic.HashSet<string> beaMideast = new System.Collections.Generic.HashSet<string>(); System.Collections.Generic.HashSet<string> censusMidAtlantic = new System.Collections.Generic.HashSet<string>(); censusMidAtlantic.Add("New York"); censusMidAtlantic.Add("New Jersey"); censusMidAtlantic.Add("Pennsylvania"); beaMideast.Add("Pennsylvania"); beaMideast.Add("New York"); beaMideast.Add("New Jersey"); } @if (beaMideast.IsSupersetOf(censusMidAtlantic) == true) { <p>The Mid-Atlantic region of the Bureau of Economic Analysis is a super-set of the Mideast region of the Census Bureau.</p> } else { <p>The Mid-Atlantic region of the Bureau of Economic Analysis is NOT a super-set of the Mideast region of the Census Bureau.</p> } @if (beaMideast.IsProperSupersetOf(censusMidAtlantic) == true) { <p>The Mid-Atlantic region of the Bureau of Economic Analysis is a PROPER super-set of the Mideast region of the Census Bureau.</p> } else { <p>The Mid-Atlantic region of the Bureau of Economic Analysis is NOT a PROPER super-set of the Mideast region of the Census Bureau.</p> } @{ beaMideast.Add("Delaware"); beaMideast.Add("District of Columbia"); beaMideast.Add("Maryland"); } @if (beaMideast.IsProperSupersetOf(censusMidAtlantic) == true) { <p>The Mid-Atlantic region of the Bureau of Economic Analysis is a PROPER super-set of the Mideast region of the Census Bureau.</p> } else { <p>The Mid-Atlantic region of the Bureau of Economic Analysis is NOT a PROPER super-set of the Mideast region of the Census Bureau.</p> } </body> </html>
This would produce:
A Union of Two Sets
Imagine you have two sets A and B defined as follows:
A = { Colorado, Montana, Wyoming, Utah, New Mexico, Arizona, Nevada, Idaho } B = { Wyoming, Colorado, Oklahoma, Kansas, Utah }
One of the operations you can perform on them is to create a set that contains a list of all their elements. That is, you would get a list that contains elements that are present in each set. If there is an element present in both sets, it would be included only once.
A union of two sets is the list of all elements that belong to both sets. Remember that there is no reason to include an element twice if it already belongs to one of the sets. In algebra, this operation is written as follows:
A ∪ B = { x|x ∈ A or x ∈ B }
This can be visually illustrated as follows:
To help you unite two sets, the HashSet class is equipped with a method named Union. Its syntax is:
public void UnionWith(IEnumerable<T> other);
This method takes as argument the set whose elements would be combined to those of the set that called the method. Here is an example of calling this method:
<!DOCTYPE html>
<html>
<head>
<title>Country's Regions</title>
</head>
<body>
<h2>Country's Regions</h2>
@{
string census = string.Empty;
string circuit = string.Empty;
System.Collections.Generic.HashSet<string> censusMountain = new System.Collections.Generic.HashSet<string>();
System.Collections.Generic.HashSet<string> courtOfAppealsCircuit10 = new System.Collections.Generic.HashSet<string>();
censusMountain.Add("Colorado");
censusMountain.Add("Montana");
censusMountain.Add("Wyoming");
censusMountain.Add("Utah");
censusMountain.Add("New Mexico");
censusMountain.Add("Arizona");
censusMountain.Add("Nevada");
censusMountain.Add("Idaho");
courtOfAppealsCircuit10.Add("New Mexico");
courtOfAppealsCircuit10.Add("Wyoming");
courtOfAppealsCircuit10.Add("Colorado");
courtOfAppealsCircuit10.Add("Oklahoma");
courtOfAppealsCircuit10.Add("Kansas");
courtOfAppealsCircuit10.Add("Utah");
}
@foreach (string state in censusMountain)
{
census += state + ", ";
}
<p><b>Census Bureau - Mountain:</b> @census.Substring(0, @census.Length - 2)</p>
@foreach (string state in courtOfAppealsCircuit10)
{
circuit += state + ", ";
}
<p><b>Court of Appeals - Circuit 10:</b> @circuit.Substring(0, @circuit.Length - 2)</p>
@{
censusMountain.UnionWith(courtOfAppealsCircuit10);
}
<h4>Union of Mountain Region of Census and Court of Appeals Circuit10</h4>
<ul>
@foreach (string state in censusMountain)
{
<li>@state</li>
}
</ul>
</body>
</html>
This would produce:
It is important to know that this method performs a comparison on the elements of both sets so that the result would not allow any duplicate item.
A Universal Set
If you have two or more sets, a universal set is a list that contains all elements of those sets. For example, if you have three sets A, B, and C, you can write their universal set as:
A ∪ B ∪ C
To programmatically create a universal set, simply call the HashSet<>.UnionWith() method as necessary.
An Intersection of Sets
The intersection between two sets is the list of only members that belong to both sets. That is, the elements that belong to one set but do not belong to the other set are excluded. In algebra, we write it as follows:
A ∩ B = { x|x ∈ A and x ∈ B }
This can be illustrated as follows:
To help you reduce a set to its intersecting elements with regards to another set, the HashSet class is equipped with a method named IntersectWith(). Its syntax is:
public void IntersectWith(IEnumerable<T> other);
This method takes a HashSet collection as argument. It finds the elements from the argument that are (the elements) also present in the variable that called it and it removes the elements that are found in the variable that called it but are not found in the argument. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Country's Regions</title>
</head>
<body>
<h2>Country's Regions</h2>
@{
string census = string.Empty;
string circuit = string.Empty;
System.Collections.Generic.HashSet<string> censusMountain = new System.Collections.Generic.HashSet<string>();
System.Collections.Generic.HashSet<string> courtOfAppealsCircuit10 = new System.Collections.Generic.HashSet<string>();
censusMountain.Add("Colorado");
censusMountain.Add("Montana");
censusMountain.Add("Wyoming");
censusMountain.Add("Utah");
censusMountain.Add("New Mexico");
censusMountain.Add("Arizona");
censusMountain.Add("Nevada");
censusMountain.Add("Idaho");
courtOfAppealsCircuit10.Add("New Mexico");
courtOfAppealsCircuit10.Add("Wyoming");
courtOfAppealsCircuit10.Add("Colorado");
courtOfAppealsCircuit10.Add("Oklahoma");
courtOfAppealsCircuit10.Add("Kansas");
courtOfAppealsCircuit10.Add("Utah");
}
@foreach (string state in censusMountain)
{
census += state + ", ";
}
<p><b>Census Bureau - Mountain:</b> @census.Substring(0, @census.Length - 2)</p>
@foreach (string state in courtOfAppealsCircuit10)
{
circuit += state + ", ";
}
<p><b>Court of Appeals - Circuit 10:</b> @circuit.Substring(0, @circuit.Length - 2)</p>
@{
censusMountain.IntersectWith(courtOfAppealsCircuit10);
}
<p>The common states between Mountain Region of Census and Court of Appeals Circuit10 are:</p>
<ul>
@foreach (string state in censusMountain)
{
<li>@state</li>
}
</ul>
</body>
</html>
This would produce:
Overlapped Sets
As seen above, the intersecting set is a list of elements shared by two sets. The comparison consists of finding out whether two lists intersect; that is, whether they have at least one common member. To support this comparison, the the HashSet class is equipped with a method named Overlaps. Its syntax is:
public bool Overlaps(IEnumerable<T> other);
This method too takes a HashSet object as argument and compares it to the object that called it. If it finds at least one value that exists in both lists, it returns true. If there is no single value that both lists share, the method returns false. Here is an example:
<!DOCTYPE html> <html> <head> <title>Country's Regions</title> </head> <body> <h2>Country's Regions</h2> @{ string bea = string.Empty; string census = string.Empty; string circuit = string.Empty; System.Collections.Generic.HashSet<string> beaMideast = new System.Collections.Generic.HashSet<string>(); System.Collections.Generic.HashSet<string> censusMountain = new System.Collections.Generic.HashSet<string>(); System.Collections.Generic.HashSet<string> courtOfAppealsCircuit10 = new System.Collections.Generic.HashSet<string>(); beaMideast.Add("Pennsylvania"); beaMideast.Add("New York"); beaMideast.Add("New Jersey"); censusMountain.Add("Colorado"); censusMountain.Add("Montana"); censusMountain.Add("Wyoming"); censusMountain.Add("Utah"); censusMountain.Add("New Mexico"); censusMountain.Add("Arizona"); censusMountain.Add("Nevada"); censusMountain.Add("Idaho"); courtOfAppealsCircuit10.Add("New Mexico"); courtOfAppealsCircuit10.Add("Wyoming"); courtOfAppealsCircuit10.Add("Colorado"); courtOfAppealsCircuit10.Add("Oklahoma"); courtOfAppealsCircuit10.Add("Kansas"); courtOfAppealsCircuit10.Add("Utah"); } @foreach (string state in beaMideast) { bea += state + ", "; } <p><b>Bureau of Economic Analysis - Mideast Region:</b> @bea.Substring(0, @bea.Length - 2)</p> @foreach (string state in censusMountain) { census += state + ", "; } <p><b>Census Bureau - Mountain:</b> @census.Substring(0, @census.Length - 2)</p> @foreach (string state in courtOfAppealsCircuit10) { circuit += state + ", "; } <p><b>Court of Appeals - Circuit 10:</b> @circuit.Substring(0, @circuit.Length - 2)</p> @if (censusMountain.Overlaps(courtOfAppealsCircuit10) == true) { <p>The Mountain Region of the Census Bureau and the 10th Circuit of the Court of Appeals have at least one state in common.</p> } else { <p>The Mountain Region of the Census Bureau and the 10th Circuit of the Court of Appeals have NO state in common.</p> } @if (beaMideast.Overlaps(courtOfAppealsCircuit10) == true) { <p>The Mideast region of the Bureau of Economic Analysis and the 10th Circuit of the Court of Appeals have at least one state in common.</p> } else { <p>The Mideast region of the Bureau of Economic Analysis and the 10th Circuit of the Court of Appeals DO NOT have ANY state in common.</p> } </body> </html>
This would produce:
The Difference of Two Sets
Consider two sets A and B. The difference between a set A from a set B is the list of elements that are found in set A but are not found in set B. In algebra, this is written as follows:
A - B = { x|x ∈ A and x ∉ B }
This can be illustrated as follows:
To let you perform this operation, the HashSet class provides a method named ExceptWith. Its syntax is:
public void ExceptWith(IEnumerable<T> other);
Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Country's Regions</title>
</head>
<body>
<h2>Country's Regions</h2>
@{
string bea = string.Empty;
string census = string.Empty;
string circuit = string.Empty;
System.Collections.Generic.HashSet<string> censusMountain = new System.Collections.Generic.HashSet<string>();
System.Collections.Generic.HashSet<string> courtOfAppealsCircuit10 = new System.Collections.Generic.HashSet<string>();
censusMountain.Add("Colorado");
censusMountain.Add("Montana");
censusMountain.Add("Wyoming");
censusMountain.Add("Utah");
censusMountain.Add("New Mexico");
censusMountain.Add("Arizona");
censusMountain.Add("Nevada");
censusMountain.Add("Idaho");
courtOfAppealsCircuit10.Add("New Mexico");
courtOfAppealsCircuit10.Add("Wyoming");
courtOfAppealsCircuit10.Add("Colorado");
courtOfAppealsCircuit10.Add("Oklahoma");
courtOfAppealsCircuit10.Add("Kansas");
courtOfAppealsCircuit10.Add("Utah");
}
@foreach (string state in censusMountain)
{
census += state + ", ";
}
<p><b>Census Bureau - Mountain:</b> @census.Substring(0, @census.Length - 2)</p>
@foreach (string state in courtOfAppealsCircuit10)
{
circuit += state + ", ";
}
<p><b>Court of Appeals - Circuit 10:</b> @circuit.Substring(0, @circuit.Length - 2)</p>
@{
censusMountain.ExceptWith(courtOfAppealsCircuit10);
}
<p>Exception Between Mountain region of the Census Bureau and Court of Appeals Circuit 10:</p>
<ul>
@foreach (string state in censusMountain)
{
<li>@state</li>
}
</ul>
</body>
</html>
This would produce:
Symmetric Difference
Consider two sets A and B. The symmetric difference between those sets is the list of elements that belong to each set but do not belong to their intersection. This can be illustrated as follows:
As you can guess, this is the reverse of the intersection. That is, it the union of the sets excluding their intersection. To let you perform this operation, the HashSet is equipped with a method named SymmetricExceptWith. Its syntax is:
public void SymmetricExceptWith(IEnumerable<T> other);
Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Country's Regions</title>
</head>
<body>
<h2>Country's Regions</h2>
@{
string census = string.Empty;
string circuit = string.Empty;
System.Collections.Generic.HashSet<string> censusMountain = new System.Collections.Generic.HashSet<string>();
System.Collections.Generic.HashSet<string> courtOfAppealsCircuit10 = new System.Collections.Generic.HashSet<string>();
censusMountain.Add("Colorado");
censusMountain.Add("Montana");
censusMountain.Add("Wyoming");
censusMountain.Add("Utah");
censusMountain.Add("New Mexico");
censusMountain.Add("Arizona");
censusMountain.Add("Nevada");
censusMountain.Add("Idaho");
courtOfAppealsCircuit10.Add("New Mexico");
courtOfAppealsCircuit10.Add("Wyoming");
courtOfAppealsCircuit10.Add("Colorado");
courtOfAppealsCircuit10.Add("Oklahoma");
courtOfAppealsCircuit10.Add("Kansas");
courtOfAppealsCircuit10.Add("Utah");
}
@foreach (string state in censusMountain)
{
census += state + ", ";
}
<p><b>Census Bureau - Mountain:</b> @census.Substring(0, @census.Length - 2)</p>
@foreach (string state in courtOfAppealsCircuit10)
{
circuit += state + ", ";
}
<p><b>Court of Appeals - Circuit 10:</b> @circuit.Substring(0, @circuit.Length - 2)</p>
@{
censusMountain.SymmetricExceptWith(courtOfAppealsCircuit10);
}
<p>Symmetric Exception Between Mountain region of the Census Bureau and Court of Appeals Circuit 10:</p>
<ul>
@foreach (string state in censusMountain)
{
<li>@state</li>
}
</ul>
</body>
</html>
This would produce:
Deleting Items From a Set
Deleting an Item
The HashSet class implements the ICollection<> interface. As such, it inherits the Remove() method that allows you to delete an item from the list. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>States</title>
</head>
<body>
<h2>States</h2>
@{
System.Collections.Generic.HashSet<string> eastCoast = new System.Collections.Generic.HashSet<string>();
eastCoast.Add("New Hampshire");
eastCoast.Add("Vermont");
eastCoast.Add("Massachusetts");
eastCoast.Add("Rhode Island");
eastCoast.Add("Connecticut");
eastCoast.Add("New Jersey");
eastCoast.Add("Delaware");
eastCoast.Add("Maryland");
eastCoast.Add("Virginia");
eastCoast.Add("North Carolina");
eastCoast.Add("South Carolina");
eastCoast.Add("Georgia");
eastCoast.Add("Florida");
}
<ul>
@foreach (string state in eastCoast)
{
<li>@state</li>
}
</ul>
@{
eastCoast.Remove("Delaware");
}
<ul>
@foreach (string state in eastCoast)
{
<li>@state</li>
}
</ul>
</body>
</html>
This would produce:
In some cases, you may want to delete an element based on a specific condition. To support this operation, the HashSet class provides a method named RemoveWhere. Its syntax is:
public int RemoveWhere(Predicate<T> match);
This method takes as argument a delegate that specifies what condition would be applied to any element that must be removed. Here is an example that asks the compiler to remove strings that contain a space:
<!DOCTYPE html>
<html>
<head>
<title>States</title>
</head>
<body>
<h2>States</h2>
@{
System.Collections.Generic.HashSet<string> eastCoast = new System.Collections.Generic.HashSet<string>();
eastCoast.Add("New Hampshire");
eastCoast.Add("Vermont");
eastCoast.Add("Massachusetts");
eastCoast.Add("Rhode Island");
eastCoast.Add("Connecticut");
eastCoast.Add("New Jersey");
eastCoast.Add("Delaware");
eastCoast.Add("Maryland");
eastCoast.Add("Virginia");
eastCoast.Add("North Carolina");
eastCoast.Add("South Carolina");
eastCoast.Add("Georgia");
eastCoast.Add("Florida");
}
<ul>
@foreach (string state in eastCoast)
{
<li>@state</li>
}
</ul>
@{
eastCoast.RemoveWhere(name => name.Contains(" "));
}
<ul>
@foreach (string state in eastCoast)
{
<li>@state</li>
}
</ul>
</body>
</html>
Clearing a Set
As an implementer of the ICollection interface, the HashSet class inherits the Clear() method that can be used to remove all items from a set.
|
||
Previous | Copyright © 2011-2021, FunctionX | Next |
|