C#编程(五十五)----------HashSet和SortedSet

饱含不重复元素的集合称为”集(set)”. .NET4包含两个集(HashSet<T>SortedSet<T>),他们都实现ISet<T>接口.HashSet<T>即包含不重复元素的无序列表,SortedSet<T>集包含不重复元素的有序列表.

ISet<T>接口提供的方法可以创建合集,交集,或者给出一个集合时另一个集的超集或子集的信息.

案例:

            //使用HashSet:重复的元素自动被移除,但是不排序

            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();

同样的代码,HashSet换成SortedSet:

            //使用SortedSet:重复的元素自动被移除,还进行了排序

            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();

总结:

1.HashSetSortedSet主要的作用是用来进行两个集合求交集,并集,差集等运算集合中包含一组不重复出现且无特性顺序的元素.前者不会自动排序,后者会加入元素后,自动排序

2.两者都无法从特定位置访问其中某个元素.

3.可以使用其查找功能:

Set.Contains(“value”) //返回truefalse

4.对集合的操作:

a . SymmetricExceptWith:仅包含该对象或指定集合中存在的元素(但不可同时包含两者中的元素).去除交集,剩下两个集合元素.

b . UnionWith:包含该对象本身和制定集合中存在的所有元素并集

c . ExceptWith从当前HashSet<T>对象中移除指定集合中的所有元素 差集

d . IntersectWith:仅包含该对象和指定集合中存在的元素交集

5.SortedSet对象,可以调用GetViewBetween,Max,Min方法

6.除了SortedSet,System.Collections.Generic命名空间下,还提供了SortedDictionarySortedList两个类.

测试HashSet内置的一些方法:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace 

{

    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);   //把 setAsetB 各自特有、对方没有的元素列出来

            Show("setA after Symmetric difference with SetB: ", setA);

            setA.UnionWith(setB);       //把 setAsetB 的全部元素列出来 (union 并集)

            Show("setA after union with setB: ", setA);

            setA.ExceptWith(setB);      //把 setA 中,所拥有的 setB 元素移除

            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();

        }

    }

}

测试SortedSet的方法:

using System;

using System.Collections.Generic;

using System.Linq;//此为Max(),Min()方法的必要调用

using System.Text;

using System.Threading.Tasks;

namespace 

{

    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>取 2 ~ 5 之间的值: ");

            //只取值为 2 ~ 5 之间的元素

            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();

        }

    }

}

 

原文地址:https://www.cnblogs.com/android-blogs/p/6609966.html